【问题标题】:PHP: file_get_contents() HTTP/1.0 400 Bad Request from google maps apiPHP:file_get_contents() HTTP/1.0 400 来自谷歌地图 api 的错误请求
【发布时间】:2023-03-17 05:22:01
【问题描述】:

我想发出一个 http 请求,但我总是得到 ​​p>

打开流失败:HTTP 请求失败! HTTP/1.0 400 错误请求

这是我的请求代码

$json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=Einsteinufer63,10587,Berlin&key=AIzaSyDBA9EWB_zNWC6XjDu9mGyIuuV6QSL_ABM');

var_dump($json);

【问题讨论】:

  • 请试试这个代码:<?php $json = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address=Einsteinufer63,10587,Berlin&key=AIzaSyDBA9EWB_zNWC6XjDu9mGyIuuV6QSL_ABM"); echo "<pre>"; var_dump($json);die;?>
  • 如果您在浏览器中手动加载 https://maps.googleapis.com/maps/api/geocode/json?address=Einsteinufer63,10587,Berlin&key=AIzaSyDBA9EWB_zNWC6XjDu9mGyIuuV6QSL_ABM 或使用 wget/curl 会发生什么?

标签: php google-maps


【解决方案1】:

这是我使用 urlencode() 函数作为地址的修改代码 因为如果您在地址中有空格或某些特殊字符,那么 google api 将无法通过上述方式工作

<?php
    $json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode("Einsteinufer63,10587,Berlin").'&key=AIzaSyDBA9EWB_zNWC6XjDu9mGyIuuV6QSL_ABM');
    var_dump($json);
?>

【讨论】:

    猜你喜欢
    • 2017-06-22
    • 2022-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多