0

I am using below script for showing google map.
it's working fine but sometimes I am getting a warning message:

<?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; ?>"/>

A PHP Error was encountered

Severity: Warning

Message: 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): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden.

Maybe there is security concern with file_get_contents when used multiple times. So help me for providing an alternate solution in place of file_get_content or How I can handle this warning.

Thanks for your support in advance.

Dave
  • 3,073
  • 7
  • 20
  • 33
Manraj
  • 205
  • 1
  • 3
  • 10
  • 1
    is this similar to http://stackoverflow.com/questions/17302757/google-maps-api-v3-error-403-forbidden-access-for-too-many-pageviews ? – WEBjuju Nov 25 '16 at 05:27
  • @WEBjuju,Thanks for your help.I have checked your shared URL but I am not getting my solution as I need to show map by using latitude and longitude. – Manraj Nov 25 '16 at 05:35
  • having used google maps api a good bit, if too many calls happen in a short time, some of them fail due to rate limiting. is that a possibility in this case? – WEBjuju Nov 25 '16 at 05:37
  • Yes, I agree with you,Is any API which is providing free/paid with unlimited calls. OR Is any way to handled this warning message ? – Manraj Nov 25 '16 at 05:40
  • have we established that the warning message is coming from rate limiting or is it from hitting a daily limit or are we unsure if either? – WEBjuju Nov 25 '16 at 05:43
  • @WEBjuju, I am unsure it comes randomly.Is any way like by using try-catch/other way for handle this error so that I can handle at least PHP encounter error. – Manraj Nov 25 '16 at 05:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128994/discussion-between-manraj-and-webjuju). – Manraj Nov 25 '16 at 05:49

1 Answers1

1

Instead of the terse

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

separate them and handle the empty image, perhaps by using a default image

$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;
}

Also, you may find more information on 403 with file_get_contents here

Community
  • 1
  • 1
WEBjuju
  • 5,797
  • 4
  • 27
  • 36