2012-12-27 4 views
6

상점 위치 정보를 수집 중입니다. 검색이 : 나는 문제없이 JSON을 반환Google 지역 정보 API : next_page_token 오류

<?php 
... 
$url='https://maps.googleapis.com/maps/api/place/search/json?key=[my_key]&location=40.420989,-3.706812&radius=1000030&=&sensor=false'; 
$body=file_get_contents($url); 
... 
?> 

, 결과의 다른 페이지가 있음을 나타냅니다. 나는 내가 오류를

'상태'를 얻을 두 번째 호출을 실행하면

<?php 
... 
$url2='https://maps.googleapis.com/maps/api/place/search/json?key=[my_key]&pagetoken=ClREAAAAQXKNHPVGCkTC_MdjSqi2T0KBDMWjEu4KF1Ylw1761Po-67AnNSp4zw0wXD4oocGpx4olSl4k2LyklJBl3mBF4VPmxp3IoOCHDRlXVmivaDsSEBuG_V1GvCH1gS5s0LCuy3EaFNXxUzzHhZ5ZRYNfeGHuqewR6Zk7&sensor=false'; 
$body=file_get_contents($url2); 
... 
?> 

을 다음과 같이 다른 전화를 걸 다시 수 있습니다 - INVALID_REQUEST

>하지만 난을 붙여 넣을 때 결과에서 ulr2 브라우저가 정확합니다.

어떻게 해결할 수 있습니까?

감사합니다.

+0

는 했나 문제를 알아 낸 그것은 또한 나에게 요청을 반환 거부 된 오류 –

답변

-2

첫 번째 쿼리는 두 번째 페이지 토큰을 생성합니다. uri에 "& pagetoken = tokenvalue"를 추가하기 만하면됩니다.

물론 작동합니다. 대체 옵션이 없습니다.

+0

나는 그것을하지만, 실행되지 않습니다. – user1932300

+0

그는 이미 자신의 URL에 pagetoken을 추가했으며 솔루션은 후속 요청 간의 시간 지연을 만들고 있으며 여기 (https://developers.google.com/places/documentation/search#PlaceSearchPaging)는이를 확인하는 사과 공식 문서입니다. –

38

요청 사이의 타이밍과 관련이 있습니다. 다른 요청을 즉시 실행하고 pagetoken이 유효하지 않은 경우 연속 요청간에 몇 초 기다려야합니다.

Google의 라이센스 조항에 따라 모든 결과를 한 번에 가져와 한꺼번에 사용자에게 반환 할 수 없기 때문입니다. 더 많은 결과를 요구하는 사용자 조치가 있어야하는데, 이는 몇 초의 지연을 추가합니다. 요청 사이의 지연에 대한 요청 사이

+0

이 답변은 올바른 것으로 표시되어야합니다 .. 내 하루 .. – allemattio

+0

@allemattio 네가 맞다.이 답변은 정확한 것으로 표시되어야하고 내 하루도 절약해야한다. –

+0

이것은 정답입니다. 요청 전에 5 초간 수면을 추가하면 모두 잘됩니다! –

5

수면 (2) 코드 아래 보시기 바랍니다 문제를

+0

멋진 솔루션 !! –

1

를 해결할 것입니다, 내가 사용한 수면 (2) 함수, 다음 페이지 토큰은 구글 서버에서 검증 될 필요가 있기 때문이다. 루프를 사용하여 코드 반복을 제거 할 수도 있습니다.

는 // 여기에 질의는

$query = ""; 

// 여기에 API 키는

$api_key = ""; 

// API 호출 코드는

try { 
     echo $url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=" . $query . "&location=40.420989,-3.706812&radius=1000030&=&sensor=false&key=" . $api_key; 
     echo "<br>"; 
     $result = file_get_contents($url); 
     $query_results = json_decode($result, true); 
     echo "First set" . "<br>"; 
     print_r($query_results); 
     $next_page_token = $query_results['next_page_token']; 
     unset($query_results); 
     $query_results = array(); 

     sleep(2); 
     echo $url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=" . $query . "&location=40.420989,-3.706812&radius=1000030&=&sensor=false&key=" . $api_key . "&pagetoken=" . $next_page_token; 
     echo "<br>"; 
     $result = file_get_contents($url); 
     $query_results = json_decode($result, true); 
     echo "Second set" . "<br>"; 
     print_r($query_results); 
     $next_page_token = $query_results['next_page_token']; 
     unset($query_results); 
     $query_results = array(); 

     sleep(2); 
     echo $url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=" . $query . "&location=40.420989,-3.706812&radius=1000030&=&sensor=false&key=" . $api_key . "&pagetoken=" . $next_page_token; 
     echo "<br>"; 
     $result = file_get_contents($url); 
     $query_results = json_decode($result, true);  
     echo "Third set" . "<br>"; 
     print_r($query_results); 
     unset($query_results); 
     $query_results = array(); 
    } catch (Exception $e) { 
     $e->getCode(); 
     $e->getLine(); 
    }