Issue with Google Distance Matrix URL while making off-browser requests

Viewed 479

https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=Neuh%C3%B6fer+Damm+110,Germany&destinations=Vogelweide+8,Hamburg&key=YOUR_API_KEY

The API Key is having the distance matrix API linked to it. If you use your own key, you will see the request in the browser working properly. However, the moment you make an HTTP::get(<>) in the code it returns you 404. Currently, I'm using PHP Laravel and follow the documentation.

Here is the request code:

 $response = Http::get('https://maps.googleapis.com/maps/api/distancematrix/json
                ?units=metric
                &origins=Neuh%C3%B6fer+Damm+110,Germany
                &destinations=Vogelweide+8,Hamburg
                &key=MyKey`enter code here`'
            );

and here is the given response:

Illuminate\Http\Client\Response {#576 ▼
  #response: GuzzleHttp\Psr7\Response {#618 ▼
    -reasonPhrase: "Not Found"
    -statusCode: 404
    -headers: array:8 [▼
      "Date" => array:1 [▶]
      "Content-Type" => array:1 [▶]
      "Server" => array:1 [▶]
      "Content-Length" => array:1 [▶]
      "X-XSS-Protection" => array:1 [▶]
      "X-Frame-Options" => array:1 [▶]
      "Server-Timing" => array:1 [▶]
      "Alt-Svc" => array:1 [▶]
    ]
    -headerNames: array:8 [▶]
    -protocol: "1.1"
    -stream: GuzzleHttp\Psr7\Stream {#617 ▶}

Are you experiencing the same issue? The main question then is: What do we miss so we get NotFound when we use the same valid link? Maybe headers? Maybe something else? I'll keep searching for an more proper answer, but meanwhile, I hope that helps

1 Answers
  1. Do not use new-lines as the empty space might be added to the request.
  2. Instead of tossing in the code one huge unreadable line of URL with tons of parameters, try using the GET Request Query Parameters. I think most HTTP clients can do that. In the case of using Laravel PHP Framework, the whole thing should look like that:
 $response = Http::get('https://maps.googleapis.com/maps/api/distancematrix/json', [
                'units' => 'metric',
                'origins' => 'Neuhöfer Damm 110,Germany',
                'destinations' => 'Vogelweide 8,Hamburg',
                'key' => YOUR_API_KEY'
            ]);

Enjoy your coding :)

Related