Google Maps PHP CURL问题“您的客户发出了格式错误或非法的请求。这就是我们所知道的。” [英] Google Maps PHP CURL issue "Your client has issued a malformed or illegal request. That’s all we know."

查看:139
本文介绍了Google Maps PHP CURL问题“您的客户发出了格式错误或非法的请求。这就是我们所知道的。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,该函数使用CURL和Google Maps API返回地址的坐标。



代码如下:

  function get_coordinates($ address_string){

$ address = urlencode($ address_string);
$ url = https://maps.googleapis.com/maps/api/geocode/json?address=。 $ address。 & key =。 $ api_key;

$ 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($ response);
$ status = $ response_a->状态;

返回$响应;

}

该代码对我和99%的网络正常我在其上使用的服务器-但是大约有1%的服务器Google会返回错误消息:

 您的客户端发出了格式错误或非法要求。我们知道的就这些。 

我已经检查并且Google API密钥正确,启用了PHP CURL并且PHP版本匹配



有人能想到可能导致Google返回此消息的任何其他情况吗?

解决方案

由于您的$ address变量的地址带有空格,请使用str_replace并将空格替换为+号。 / p>

它将起作用,我遇到了同样的问题,并采用这种方式解决了。


I have a function which returns the coordinates of an address using CURL and the Google Maps API.

The code is as follows:

function get_coordinates($address_string) {

    $address = urlencode($address_string);
    $url = "https://maps.googleapis.com/maps/api/geocode/json?address=" . $address . "&key=" . $api_key;

    $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($response);
    $status = $response_a->status;

    return $response;

}

The code works fine for me and 99% of the web servers I use it on - but for around 1% of servers Google returns the error message:

Your client has issued a malformed or illegal request. That’s all we know.

I've checked and the Google API key is correct, PHP CURL is enabled and the PHP version matches PHP versions it is working on.

Can anyone think of any other things which could be causing Google to return this message?

解决方案

Its Because of your $address variable has address with white space, use str_replace and replace the white space with + sign.

It will work, i was facing same problem and fixed this way.

这篇关于Google Maps PHP CURL问题“您的客户发出了格式错误或非法的请求。这就是我们所知道的。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆