如何输出google api搜索中的formatted_address?

时间:2012-07-06 06:03:20

标签: php curl

我正在尝试输出渥太华输入酒店的格式化地址。结果给了我所需的所有数据以及不需要的东西。我需要截断这个。有任何想法吗?

     $address = urlencode("Hotels, in Ottawa");

     $geocodeURL = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=hotels+in+Ottawa&sensor=false&key=mykey";
     $ch = curl_init($geocodeURL);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     $result = curl_exec($ch);
     $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     curl_close($ch);

     $geocode = json_decode($result);

     echo $result;

     //echo $formatted_address = $geocode->data[0]->formatted_address;

1 个答案:

答案 0 :(得分:2)

看来我终于弄明白了。

 $geocodeURL = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=hotels+in+Ottawa&sensor=false&key=mykey";
 $ch = curl_init($geocodeURL);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 $results = curl_exec($ch);
 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 curl_close($ch);

 $geocode = json_decode($results);

 for($i=0; $i<20; $i++){
 echo $formatted_address = $geocode->results[$i]->formatted_address;
 }