使用googlemaps的php服务器端地理编码返回状态OK,但不返回任何其他内容
我正在尝试使用php在服务器端进行地理编码。以下是我的代码:使用googlemaps的php服务器端地理编码返回状态OK,但不返回任何其他内容,php,google-maps,curl,Php,Google Maps,Curl,我正在尝试使用php在服务器端进行地理编码。以下是我的代码: static private function curl_file_get_contents($URL){ $c = curl_init(); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); curl_setopt($c, CURLOPT_URL, $URL); $contents = curl_exec($c); curl_close($c);
static private function curl_file_get_contents($URL){
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
$contents = curl_exec($c);
curl_close($c);
if ($contents) return $contents;
else return FALSE;
}
public function preview(Request $request)
{
$url = "https://maps.googleapis.com/maps/api/geocode/json?key=my_api_key&address=1600+Amphitheatre+Parkway,+Mountain+View,+CA";
$resp_json = self::curl_file_get_contents($url);
$resp = json_decode($resp_json, true);
if($resp['status']='OK'){
dd($resp);
}else{
dd('not ok');
}
当我运行此程序时,我的状态为OK,dd$resp;执行并输出以下内容:
array:1 [▼
"status" => "OK"
]
我在这里查看了json响应示例:
还有更多的东西应该归还,但在我的情况下,它没有被归还。我不知道为什么,这可能与我在本地运行这个有什么关系,尽管在这种情况下,它不应该返回状态:OK
我把这个例子搁置在这里
编辑:
如果我直接进入浏览器
然后它返回更多的数据
{
"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Parkway",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "USA",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.4223664,
"lng" : -122.084406
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4237153802915,
"lng" : -122.0830570197085
},
"southwest" : {
"lat" : 37.42101741970851,
"lng" : -122.0857549802915
}
}
},
"place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
我通过添加
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
请检查您的服务器配置,并检查是否支持curl。 使用此代码检查服务器配置
<?php
function get_tls_version($sslversion = null)
{
$c = curl_init();
curl_setopt($c, CURLOPT_URL, "https://www.howsmyssl.com/a/check");
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
if ($sslversion !== null) {
curl_setopt($c, CURLOPT_SSLVERSION, $sslversion);
}
$rbody = curl_exec($c);
if ($rbody === false) {
$errno = curl_errno($c);
$msg = curl_error($c);
curl_close($c);
return "Error! errno = " . $errno . ", msg = " . $msg;
} else {
$r = json_decode($rbody);
curl_close($c);
return $r->tls_version;
}
}
echo "<pre>\n";
echo "OS: " . PHP_OS . "\n";
echo "uname: " . php_uname() . "\n";
echo "PHP version: " . phpversion() . "\n";
$curl_version = curl_version();
echo "curl version: " . $curl_version["version"] . "\n";
echo "SSL version: " . $curl_version["ssl_version"] . "\n";
echo "SSL version number: " . $curl_version["ssl_version_number"] . "\n";
echo "OPENSSL_VERSION_NUMBER: " . dechex(OPENSSL_VERSION_NUMBER) . "\n";
echo "TLS test (default): " . get_tls_version() . "\n";
echo "TLS test (TLS_v1): " . get_tls_version(1) . "\n";
echo "TLS test (TLS_v1_2): " . get_tls_version(6) . "\n";
echo "</pre>\n";
?>
如果curl不受支持,它将不会产生上述输出。