如何获得超过20个结果ie 60使用google place API与PHP?

我正在开发一个Google Places API应用程序,其中我的目标是获取特定查询的所有可用细节。但是,由于我知道谷歌API API只能一次返回20个结果。但是我需要所有可用的地方详细信息。

如果我搜索关键字不止一次,它总是给出相同的结果。所以我想知道,每次使用相同的关键字搜索时,都有任何方式获得不同的结果。

 <?php 
        $placeSearchURL = 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=' . $query . '&sensor=true' . '&key=' . API_KEY;

        $placeSearchJSON = file_get_contents($placeSearchURL);
        $dataArray = json_decode($placeSearchJSON);
        $references = array();
        $detailsOfPlaces = array();

        if (isset($dataArray->status)) {
            switch ($dataArray->status) {
                case 'OK' :
                    foreach( $dataArray->results as $details) {
                        $references[] = $details->reference;
                    }
                    break;
                default:
                    echo "Something wrong";
            }
        }

        foreach ($references as $reference) {
            $placeDetailsURL = 'https://maps.googleapis.com/maps/api/place/details/json?reference=' . $reference . '&sensor=true&key=' . API_KEY;
            $placeDetails =file_get_contents($placeDetailsURL);
            $placeDetailsAsArray = json_decode($placeDetails);

            switch ($dataArray->status) {
                case 'OK' :
                    $detailsOfPlace['business_name'] = isset($placeDetailsAsArray->result->name) ? $placeDetailsAsArray->result->name : '';
                    $detailsOfPlace['address'] = isset($placeDetailsAsArray->result->formatted_address) ? $placeDetailsAsArray->result->formatted_address : '';                      

                    $detailsOfPlaces[] = $detailsOfPlace;
                    break;
                default:
                    echo "Something went wrong";
            }
        }

这是我的代码它为每个查询返回20个结果。任何人都可以告诉我应该做什么调整,以获得每个查询的60个结果。

谢谢

正如克里斯·格林在答案中所说,你必须使用分页获得20多个结果。
还有,克里斯·格林(Chris Green)也表示要采取你真正需要再次呼吁每个地方的参考的地址。我假设你将需要更多的信息,所以我刚刚创建了两个功能来实现Chris在回应中所说的。
没有测试可能会有一些错误,但你可以得到的感觉。

<?php

    function getAllReferences($query, $maxResults= 60, $nextToken = false) {
        $references = array();
        $nextStr = "";
        if ($nextToken)
            $nextStr = "nextpage=$nextToken";
        $placeSearchURL = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=$query&sensor=true&key=".API_KEY."&$nextStr";
        $placeSearchJSON = file_get_contents($placeSearchURL);
        $dataArray = json_decode($placeSearchJSON);
        if (isset($dataArray->status) &&$dataArray->status == "OK") {
            foreach( $dataArray->results as $details) {
                array_push($references, $details->reference);
            }
            if (!empty($dataArray->next_page_token) && count($references) < $maxResults ) {
                $nextArray = getAllReferences($query, $maxResults-20 , $dataArray->next_page_token);
                $references = array_merge($references, $nextArray);

            }
            return $references;
        }
    }

    function getPlaceFromReference($reference) {
        $placeDetailsURL = 'https://maps.googleapis.com/maps/api/place/details/json?reference=' . $reference . '&sensor=true&key='.API_KEY;
        $placeDetailsContent =file_get_contents($placeDetailsURL);
        $placeDetails = json_decode($placeDetailsContent);
        if ($placeDetails->status == "OK")
            return $placeDetails;
        else return false;
    }

    $references = getAllReferences("test");
    if (count($references) == 0)
        echo "no results";
    else {
        foreach ($references as $reference) {
            $placeDetails = getPlaceFromReference($reference);
            print_r($placeDetails);
        }
    }
http://stackoverflow.com/questions/13814494/how-to-get-more-than-20-results-ie-60-using-google-place-api-with-php

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:如何获得超过20个结果ie 60使用google place API与PHP?