0

I am hoping for some direction on this error. I have a loop that connects with the google places API. Every few times I test it I get an HTTP request failed error. But when I take the API call from the error it works in a browser or in postman. Any suggestions for what might be causing this? Thanks for all of your help. Here is my API call and the error.

$googleApiPlaceUri = 'https://maps.googleapis.com/maps/api/place/textsearch/json';
        foreach ($attractions as $attraction) {
            $api_call = "{$googleApiPlaceUri}?query={$attraction}+in+{$city}&key={$api_key}";
            $api_return_array[] = json_decode(file_get_contents($api_call));
            foreach ($api_return_array as $result) {
                $results = $result->results;
                array_push($results_array, $results);
            }
        }

Here is the error I get...

Warning: file_get_contents(https://maps.googleapis.com/maps/api/place/textsearch/json?query=hiking +in+Denver&key=API_KEY): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
3
  • is API_KEY actually getting replaced with your key? I also think it could be the space between hiking +in+, notice the space after hiking. Commented Jul 27, 2017 at 20:40
  • The API key is being replaced, I just didn't post the key here. I don't think there is a problem with the spacing because if I copy the URL directly from the error, it works fine in a browser or in postman. This is also a loop and in this case, it worked fine for twice and returned this error once. I also just tried the same search terms in a different order and it worked fine. I am not sure why this error only comes up sometimes. Commented Jul 27, 2017 at 20:48
  • In the browser it's going to get URL encoded which will handle the space. I don't know about postman but it may do that also. Maybe just fix it once and try it to rule that out? Commented Jul 27, 2017 at 20:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.