php - Google Distance Matrix API error Invalid Request -
i have following code create request in php response google distance matrix api:
//get info google $map_url = 'https://maps.googleapis.com/maps/api/distancematrix/json'; $midnight = strtotime('tomorrow midnight'); $params = array( 'units' => 'metric', 'origins' => str_replace(' ', '+', str_replace('.', '', $arr['address'])), 'destinations' => str_replace(' ', '+', str_replace('.', '', $rink_arr['address'])), 'traffic_model' => 'best_guess', 'departure_time' => $midnight + $rink_arr['avg_time'], 'key' => 'api_key' ); //get data $url = $map_url.'?'.http_build_query($params); $json = file_get_contents($url); echo '<pre>'; echo $url.'<br><br>'; print_r($json); echo '</pre>'; exit();
when run code following response:
https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=2141+southview+ave%2c+innisfil%2c+on+l9s+1h4&destinations=190+bayview+dr%2c+barrie%2c+on+l5n+9b4&traffic_model=best_guess&departure_time=1479523235&key=api_key { "destination_addresses" : [], "origin_addresses" : [], "rows" : [], "status" : "invalid_request" }
but here's catch when copy url above response , paste in browser works. following response:
{ "destination_addresses" : [ "190 bayview dr, barrie, on l4n 2z4, canada" ], "origin_addresses" : [ "2141 southview ave, innisfil, on l9s 1h4, canada" ], "rows" : [ { "elements" : [ { "distance" : { "text" : "9.0 km", "value" : 9027 }, "duration" : { "text" : "13 mins", "value" : 758 }, "duration_in_traffic" : { "text" : "12 mins", "value" : 720 }, "status" : "ok" } ] } ], "status" : "ok" }
has had similar problem , know why case? have tried changing url doesn't make sense works in browser , not in file_get_contents. have tried curl , got same response. provide.
Comments
Post a Comment