Jump to content

[Inactive] Network Error With Curl Request To Google Places Api


vitae

Recommended Posts

Hello!

 

I am a new user of the Stevie server. I am setting up a script which has worked on other server (and still works elsewhere). The script attempts to access the Google Places API by sending a CURL request. However, most of the time the PHP script will error out on the Stevie server. Occasionally it will work but not typically. This script works 100% on a different server that I am migrating from. Here is the relevant code:

 

$placeName = 'XXX';

$lat = '37';

$lon = '-122';

$radius = '10000'; //Meters

$dataType = 'json'; //json or xml

$url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/';

$url .= $dataType;

$url .= '?key='.$Google_Maps_API_Key; //Stored in envoirnment.php

$url .= '&location='.$lat.','.$lon;

$url .= '&radius='.$radius;

$url .= '&name='.$placeName;

 

$curl = curl_init($url);

 

curl_setopt($curl, CURLINFO_HEADER_OUT, true);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($curl, CURLOPT_DNS_USE_GLOBAL_CACHE, false );

curl_setopt($curl, CURLOPT_DNS_CACHE_TIMEOUT, 2 );

 

echo curl_exec($curl);

 

As I said, once in a while the script will execute and the JSON will be returned. Most of the time I get an error. If I print the CURL error I get this:

 

7: Failed to connect to 2607:f8b0:400e:c03::5f: Network is unreachable

 

Other times, if I am not error checking in my code, the script will simply timeout and give HelioHosts Internal Server Error 500 page

 

If I print $url before the CURL and copy/paste the url into my browser, I get the JSON form google fine so I know it is not a malformed URL.

 

Could there by a network issue btwn Helio Host and Google?

 

Thank you for taking the time to read my post.

 

Here is the script:

 

http://vitaecafe.tk/vitae/actions.php?a=4

Link to comment
Share on other sites

Odds are cURL is to blame, not our network. It worked on 7 of the 8 I tried, the eighth gave a 500 error because of cURL. In addition, cURL is an easy way to get suspended for load. It uses a lot more resources than methods like file_get_contents (which will happily retrieve content over HTTP).

 

I'd recommend rewriting the script to not use cURL and see if it keeps acting up.

Link to comment
Share on other sites

Thank you for reviewing my post and providing me with a quick response.

 

I have rewritten the code using file_get_contents and am getting similar results (although file_get_contents doesn't throw a 500). It seems to work for a few minutes and then it tanks (returns nothing).

 

Here is the relevant code:

 

$placeName = 'XXXX';

$lat = '37';

$lon = '-122';

$radius = '10000'; //Meters

$dataType = 'json'; //json or xml

$url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/';

$url .= $dataType;

$url .= '?key='.$Google_Maps_API_Key; //Stored in envoirnment.php

$url .= '&location='.$lat.','.$lon;

$url .= '&radius='.$radius;

$url .= '&name='.$placeName;

 

$retval = file_get_contents($url);

echo $retval;

 

 

And here is the script:

http://vitaecafe.tk/vitae/actions.php?a=5

 

Thanks!

Link to comment
Share on other sites

That's odd...Krydos might know more then. I see cURL issues quite a bit, but there really should be no reason file_get_contents doesn't work reliably.

 

I'll escalate it for you, though it may take some time for Krydos to get back to you on this.

Link to comment
Share on other sites

Here's the error I get:

{ "error_message" : "This IP, site or mobile application is not authorized to use this API key. Request received from IP address 65.19.143.2, with empty referer", "html_attributions" : [], "results" : [], "status" : "REQUEST_DENIED" } { "error_message" : "This IP, site or mobile application is not authorized to use this API key. Request received from IP address 65.19.143.2, with empty referer", "html_attributions" : [], "results" : [], "status" : "REQUEST_DENIED" }

My guess is that you might have to add Stevie's IP to a safe list of some sort to use that API.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...