无法打开流:HTTP请求失败! HTTP / 1.0 403静态地图中的禁止警告错误google api

时间:2016-11-25 05:25:27

标签: php google-maps

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

<?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错误

严重性:警告

  

消息:   的file_get_contents(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或我如何处理此警告。

感谢您的支持。

1 个答案:

答案 0 :(得分: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