Google 使用 PHP 进行地理编码
Google Geocode with PHP
我正在尝试从 Google 的地理编码 API 获取纬度和经度。我正在使用的 $url
输出正确的 JSON 但是我脚本中的某些内容无法正常工作。它不输出 $lati
& $longi
变量。请查看以下脚本:
PHP:
<?php
$address = 'Tempe AZ';
$address = urlencode($address);
$url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address={$address}";
$resp_json = file_get_contents($url);
$resp = json_decode($resp_json, true);
if ($resp['status'] == 'OK') {
// get the important data
$lati = $resp['results'][0]['geometry']['location']['lat'];
$longi = $resp['results'][0]['geometry']['location']['lng'];
echo $lati;
echo $longi;
} else {
return false;
}
?>
原来是代理问题。此线程帮助:Using proxy with file_get_contents
添加了这个:
$opts = array(
'http' => array(
'method' => "GET",
'header' => "Accept-language: en\r\n".
"Cookie: foo=bar\r\n",
'proxy' => 'tcp://proxy.proxy.com:8080',
)
);
$context = stream_context_create($opts);
// open the file using the HTTP headers set above
$file = file_get_contents($url, false, $context);
你的脚本也适用于我。也许你会得到一个错误调用:
$resp_json = file_get_contents($url);
。
如果发生这种情况,您应该这样做:
$resp_json = @file_get_contents($url);
if ($resp_json === FALSE) {
///logic for exception
} else {
///do your logic hear
}
我在 Laravel file/class 的顶部引入了 Guzzle http 包:使用 GuzzleHttp\Client;
然后,为了获取地址并将其转换为纬度和经度以使用 PHP/Laravel 和 Google 地图 API 进行保存,我在 save() 方法中添加了以下代码:
// get latitude and longitude of address with Guzzle http package and Google Maps geocode API
$client = new Client(); // GuzzleHttp\Client
$baseURL = 'https://maps.googleapis.com/maps/api/geocode/json?address=';
$addressURL = urlencode(request('street')) . ',' . urlencode(request('city')) . ',' . urlencode(request('state')) . '&key=' . env('GOOGLE_MAPS_API_KEY');
$url = $baseURL . $addressURL;
$request = $client->request('GET', $url);
$response = $request->getBody()->getContents();
$response = json_decode($response);
$latitude = $response->results[0]->geometry->location->lat;
$longitude = $response->results[0]->geometry->location->lng;
我正在尝试从 Google 的地理编码 API 获取纬度和经度。我正在使用的 $url
输出正确的 JSON 但是我脚本中的某些内容无法正常工作。它不输出 $lati
& $longi
变量。请查看以下脚本:
PHP:
<?php
$address = 'Tempe AZ';
$address = urlencode($address);
$url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address={$address}";
$resp_json = file_get_contents($url);
$resp = json_decode($resp_json, true);
if ($resp['status'] == 'OK') {
// get the important data
$lati = $resp['results'][0]['geometry']['location']['lat'];
$longi = $resp['results'][0]['geometry']['location']['lng'];
echo $lati;
echo $longi;
} else {
return false;
}
?>
原来是代理问题。此线程帮助:Using proxy with file_get_contents
添加了这个:
$opts = array(
'http' => array(
'method' => "GET",
'header' => "Accept-language: en\r\n".
"Cookie: foo=bar\r\n",
'proxy' => 'tcp://proxy.proxy.com:8080',
)
);
$context = stream_context_create($opts);
// open the file using the HTTP headers set above
$file = file_get_contents($url, false, $context);
你的脚本也适用于我。也许你会得到一个错误调用:
$resp_json = file_get_contents($url);
。
如果发生这种情况,您应该这样做:
$resp_json = @file_get_contents($url);
if ($resp_json === FALSE) {
///logic for exception
} else {
///do your logic hear
}
我在 Laravel file/class 的顶部引入了 Guzzle http 包:使用 GuzzleHttp\Client;
然后,为了获取地址并将其转换为纬度和经度以使用 PHP/Laravel 和 Google 地图 API 进行保存,我在 save() 方法中添加了以下代码:
// get latitude and longitude of address with Guzzle http package and Google Maps geocode API
$client = new Client(); // GuzzleHttp\Client
$baseURL = 'https://maps.googleapis.com/maps/api/geocode/json?address=';
$addressURL = urlencode(request('street')) . ',' . urlencode(request('city')) . ',' . urlencode(request('state')) . '&key=' . env('GOOGLE_MAPS_API_KEY');
$url = $baseURL . $addressURL;
$request = $client->request('GET', $url);
$response = $request->getBody()->getContents();
$response = json_decode($response);
$latitude = $response->results[0]->geometry->location->lat;
$longitude = $response->results[0]->geometry->location->lng;