[英]How to get results names from Google Maps Api Search in PHP
我有一個像這樣的網址,可以在某個位置搜索內容:
https://maps.google.com/maps?q=dentist+Austin+Texas&hl=en&mrt=yf=l
我需要前2個結果的列表,例如(“ Dentist Mr.Example1”,“ Dentist Ex.2”)。
觀看此問題: 我應該在Google Maps URL中使用哪些參數來進行定位? ,我認為網址是正確的。
並觀看以下內容: 使用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);
在兩種情況下,我都沒有結果。 提前致謝!
我有一個像這樣的網址,可以在某個位置搜索內容:
https://maps.google.com/maps?q=dentist+Austin+Texas&hl=en&mrt=yf=l
我需要前2個結果的列表,例如(“ Dentist 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;
}
}
一些注意事項:
引用: https : //developers.google.com/maps/documentation/geocoding/
這幾乎是完美的,但我還想獲得電話號碼和排名(評價之星)。
最初的問題是使用Google Maps API列出austin,tx中牙醫獲得的第一個結果。
此附加要求更改了要使用的API /網絡服務,以檢索豐富的數據。 您需要有關地址的更多詳細信息 。 數據元素“ phone_number”和“ rating”是Google Places Webservice(位置詳細信息)的一部分 。 您需要將密鑰添加到請求URL( &key=API_KEY
)中才能訪問此服務。
https://developers.google.com/places/webservice/details#PlaceDetailsRequests
這是一個場所詳細信息請求 :
1)從第一個請求中提取“ place_id”。
2)對於后續請求,您可以通過“位置詳細信息” Web服務檢索有關每個位置的詳細信息。
示例:在這里我將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);
看看那里有什么,然后選擇您需要的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.