【问题标题】:How to get results names from Google Maps Api Search in PHP如何从 PHP 中的 Google Maps Api Search 获取结果名称
【发布时间】:2015-05-22 01:09:44
【问题描述】:

我有一个这样的网址,可以在某个位置搜索内容:

https://maps.google.com/maps?q=dentist+Austin+Texas&hl=en&mrt=yf=l

我需要前 2 个结果的列表,例如(“Dentist Mr.Example1”、“Dentist Ex.2”)。

看了这个问题:What parameters should I use in a Google Maps URL to go to a lat-lon?,我觉得url是对的。

看这个:Get latitude and longitude automatically using php, API 我尝试了这两个选项:

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($data);

还有这个:

 $content= file_get_contents($url);
preg_match_all("|<span class=\"pp-place-title\">(.*?)</span></span>|",$content,$result);

在这两种情况下,我都没有任何结果。 提前致谢!

【问题讨论】:

    标签: php google-maps


    【解决方案1】:

    我有一个这样的网址,可以在某个位置搜索内容:

    https://maps.google.com/maps?q=dentist+Austin+Texas&hl=en&mrt=yf=l

    我需要前 2 个结果的列表,例如(“牙医 Mr.Example1”,”Dentist Ex.2”)。

    这是一个 Google Maps API 请求。

    给你:

    $searchTerm = 'dentist+austin+texas';
    
    $url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . $searchTerm;
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $response = curl_exec($ch);
    curl_close($ch);
    
    $array = json_decode($response, true);
    
    // var_dump($array);
    
    // Output
    
    $array = $array['results'];
    
    foreach($array as $index => $component)
    {
        echo '#' . $index . ' ' . $component['formatted_address'] . '<br>';
    
        // show only the first 2 items (#0 & #1)
        if($index === 1) {
            break;
        }
    }
    

    一些注意事项:

    • 您的搜索词是“dentist Austin textas”,如果它是 URL 的一部分,则变为“dentist+austin+texas”。
    • searchTerm 附加到 API URL。
    • 此 URL 用于 cURL 请求。
    • 原始响应是 $response。
    • 通过将 json_decode() 的第二个参数设置为 true,将其转换为 $array。
    • 您可以使用 var_dump() 数组来查看键。或者直接在浏览器中调用这个 URL:https://maps.googleapis.com/maps/api/geocode/json?address=dentist+austin+texas
    • 对于显示部分:这是一个简单的数组迭代。您可以通过将值重新分配给 $array 来摆脱顶级“结果”数组。
    • 输出是牙医编号列表

    参考:https://developers.google.com/maps/documentation/geocoding/


    这几乎是完美的,但我还想获得电话号码和排名(评论的星星)。

    最初的问题是使用 Google Maps API 列出德克萨斯州奥斯汀牙医的第一个结果。

    此附加要求会更改要使用的 API/Web 服务,以便检索丰富的数据。您想要更多关于地址的details。 数据元素“phone_number”和“rating”是Google Places Webservice (Place Details) 的一部分。您需要将密钥添加到请求 URL (&amp;key=API_KEY) 才能访问此服务。

    https://developers.google.com/places/webservice/details#PlaceDetailsRequests

    这是一个地方详情请求:

    1) 从第一个请求中提取“place_id”。

    2) 对于后续请求,您可以通过“地点详细信息”网络服务检索每个地点的详细信息。

    示例:这里我使用placeid 作为第一个条目:

    新代码:

    <?php
    
    // Google GeoCode API
    $address = 'dentist+austin+texas';
    
    $array = getGoogleGeoCode($address);
    $array = $array['results'];
    
    //var_dump($array);
    
    foreach($array as $index => $component)
    {
        echo '#' . $index . ' ' . $component['formatted_address'] . ', ' ;
    
        // subsequent request for "Place Details"
        $details = getGooglePlaceDetails($component['place_id']);
        $details = $details['result'];
        //var_dump($details);
    
        echo 'Phone: ' . $details['formatted_phone_number']. ', ' ;
    
        // rating contains the place's rating, from 1.0 to 5.0, based on aggregated user reviews.
        if(isset($details['rating'])) {
            echo 'Rating: ' . $details['rating'];
        }
    
        // show only the first two entries
        /*if($index === 1) {
            break;
        }*/
    
        echo '<br>';
    }
    
    function getGooglePlaceDetails($placeid)
    {
        // your google API key
        $key = 'AIzaSyCj9yH5x6_5_Om8ebAO2pBlaqJZB-TIViY';
        $url = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=' . $placeid . '&key=' . $key;
    
        return curlRequest($url);
    }
    
    function getGoogleGeoCode($address)
    {
        $url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . $address;
    
        return curlRequest($url);
    }
    
    function curlRequest($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $response = curl_exec($ch);
        curl_close($ch);
    
        return json_decode($response, true);
    }
    

    结果:

    • 数组元素rating 并不总是存在,因为它是基于审查的。只需使用 var_dump($details); 即可查看其中的内容并选择您需要的内容。
    • 要减少列表,请删除 break 语句周围的 cmets。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      • 1970-01-01
      • 2021-10-27
      • 1970-01-01
      • 2014-08-27
      • 2021-11-15
      • 2021-08-23
      相关资源
      最近更新 更多