带有curl的PHP Google地理代码拒绝工作
我目前正试图通过在PHP页面中使用GoogleGeocodeAPI来获取地址的lat和lng参数 我目前有以下代码,但不知何故,它无法通过php工作,而将生成的地址复制到Google Chrome中似乎确实有效 有人能看到下面代码中的错误吗 提前谢谢 汤姆 ==================================================== 返回的错误为: 带有过时部分的旧代码: ==================================================== 编辑1-向每个网页添加HTML代码,使其正常工作 您的$url无效。不做$url=htmlspecialchars$url;在调用CURL之前 更改: // ... $url=htmlspecialchars$url; echo$url; $ch=curl\u init; curl_setopt$ch,CURLOPT_URL,$URL; // ... 至或smth: // ... echo htmlspecialchars$url; $ch=curl\u init; curl_setopt$ch,CURLOPT_URL,$URL; // ... 编辑1: 并且您的地址生成无效。而不是:带有curl的PHP Google地理代码拒绝工作,php,google-maps,curl,google-api,geocoding,Php,Google Maps,Curl,Google Api,Geocoding,我目前正试图通过在PHP页面中使用GoogleGeocodeAPI来获取地址的lat和lng参数 我目前有以下代码,但不知何故,它无法通过php工作,而将生成的地址复制到Google Chrome中似乎确实有效 有人能看到下面代码中的错误吗 提前谢谢 汤姆 ==================================================== 返回的错误为: 带有过时部分的旧代码: ================================================
$googleQuery = $_POST['txtAdres'] . ',+' . $_POST['txtPostcode'] . '+' . $_POST['txtStad'] . ',+' . $_POST['txtLand'];
$googleQuery = str_replace(' ', '+', $googleQuery);
做:
编辑2:
以下是一个有效的例子:
$googleQuery = 'Sint Elooisstraat 30, 8800 Roeselare, België';
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($googleQuery) . '&sensor=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
print_r($response);
在CLI脚本中出现此问题 我必须确保从CSV文件导入的数据是UTF-8编码的:
$address = utf8_encode($address);
$address = rawurlencode($address);
此外,我必须从脚本中传递一个字符串,在将其转换为UTF-8或htmlentities之前,我必须先将其转换为ASCII,这也适用于:
mb_convert_encoding('Österreich', 'US-ASCII', 'UTF-8')
或
删除htmlspecialchars使情况变得更糟。现在错误消息是{results:[],status:INVALID_REQUEST}您可以显示可变$url的输出吗?p、 我刚刚测试了这个脚本,它可以为我工作。返回的url带有测试数据,但没有htmlspecialchars:错误消息是:Array[results]=>Array[status]=>INVALID\u请求发生错误:1带有htmlspecialchars的url:您的地址无效。删除将空格替换为+的str_replace和将post数据合并为字符串的+的stru replace;这一切都是由urlecnode函数完成的。输出url现在是:并且错误消息仍然是:Array[results]=>Array[status]=>INVALID\u请求发生错误:1
$googleQuery = $_POST['txtAdres'] . ',+' . $_POST['txtPostcode'] . '+' . $_POST['txtStad'] . ',+' . $_POST['txtLand'];
$googleQuery = str_replace(' ', '+', $googleQuery);
$googleQuery = $_POST['txtAdres'] . ', ' . $_POST['txtPostcode'] . ' ' . $_POST['txtStad'] . ', ' . $_POST['txtLand'];
$googleQuery = 'Sint Elooisstraat 30, 8800 Roeselare, België';
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($googleQuery) . '&sensor=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
print_r($response);
$address = utf8_encode($address);
$address = rawurlencode($address);
mb_convert_encoding('Österreich', 'US-ASCII', 'UTF-8')
htmlentities('Österreich', ENT_COMPAT, 'UTF-8')