【问题标题】:Failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden warning error in staticmap google api无法打开流:HTTP 请求失败! HTTP/1.0 403 staticmap google api中的禁止警告错误
【发布时间】:2017-04-09 10:57:37
【问题描述】:

我正在使用下面的脚本来显示谷歌地图。
它工作正常,但有时我会收到一条警告消息:

<?php
$src = 'https://maps.googleapis.com/maps/api/staticmap?      center=40.714728,-73.998672&markers=color:red%7Clabel:C%7C40.718217,-73.998284&zoom=12&size=600x400';
    $time = time();
    $desFolder = '';
    $imageName = 'google-map.PNG';
    $imagePath = $desFolder.$imageName;
    file_put_contents($imagePath,file_get_contents($src));
?>
<img src="<?php echo $imagePath; ?>"/>

遇到了 PHP 错误

严重性:警告

消息: 文件获取内容(https://maps.googleapis.com/maps/api/staticmap?center=39.9834656,-75.1499542&15&size=250x250&maptype=hybrid&markers=color:red%7Ccolor:red%7Clabel:A%7C39.9834656,-75.1499542&sensor=true): 无法打开流:HTTP 请求失败! HTTP/1.0 403 禁止。

file_get_contents 在多次使用时可能存在安全问题。 因此,请帮助我提供替代解决方案来代替 file_get_content 或如何处理此警告。

提前感谢您的支持。

【问题讨论】:

  • @WEBjuju,感谢您的帮助。我已经检查了您的共享 URL,但我没有得到我的解决方案,因为我需要使用纬度和经度来显示地图。
  • 已经用过google maps api了,如果短时间内调用太多,有的会因为限速而失败。在这种情况下有这种可能吗?
  • 是的,我同意你的观点,是否有任何 API 提供免费/付费无限通话。或者有什么方法可以处理这个警告信息吗?
  • 我们是否确定警告消息是来自速率限制,还是来自达到每日限制,或者我们不确定是否是其中之一?

标签: php google-maps


【解决方案1】:

而不是简洁

file_put_contents($imagePath,file_get_contents($src));

将它们分开并处理空图像,可能使用默认图像

$google_img = file_get_contents($src);
if ($google_img === false) {
  // hmmm, how will you handle it?
  // you could do a variety of things
  $img = DEFAULT_IMG; // define default image constant elsewhere
} else {
  // yay, we have the image...continue
  file_put_contents($imagePath, $google_img);
  $img = $google_img;
}

另外,你可以find more information on 403 with file_get_contents here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    相关资源
    最近更新 更多