简体   繁体   English

为什么会出现此GeoCode错误

[英]Why am I Getting This GeoCode Error

This is what I'm getting when I try to make a post on my site. 这就是我尝试在自己的网站上发布帖子时得到的。 What do I need to do to fix? 我需要做什么来解决?

Warning: file_get_contents( http://maps.google.com/maps/api/geocode/json?address= ): failed to open stream: HTTP request failed! 警告:file_get_contents( http://maps.google.com/maps/api/geocode/json?address= ):无法打开流:HTTP请求失败! HTTP/1.0 400 Bad Request in /home/dfwwrightimc/public_html/wp-content/themes/visitdfw/functions.php on line 351 第351行的/home/dfwwrightimc/public_html/wp-content/themes/visitdfw/functions.php中的HTTP / 1.0 400错误请求

function geo_lat_long($address) {
    $address1 = str_replace(" ","+",$address);
    $address1 = str_replace("&","%26",$address1);
    $json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address1");
    $json = json_decode($json);
    $lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
    $long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
    return array('lat' => $lat, 'long' => $long);
}

Change the following lines: 更改以下行:

$address1 = str_replace(" ","+",$address);
$address1 = str_replace("&","%26",$address1);
$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address1");

to

$address1 = urlencode ( $address );
$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address1");

and try again. 然后再试一次。

Explanation: The error Bad Request is related to the incorrect URL. 说明:错误的Bad Request与错误的URL有关。

urlencode — URL-encodes string

string urlencode ( string $str )

This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page. 当编码要在URL的查询部分中使用的字符串时,此功能很方便,是将变量传递到下一页的便捷方法。

Check how you are calling the geo_lat_long() function. 检查您如何调用geo_lat_long()函数。 Is the address passed in correctly? 地址输入正确吗? Since the URL in the error message ends in ?address= with no value, it looks like $address1 is empty. 由于错误消息中的URL以?address=结尾,没有任何值,因此看起来$address1为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM