0

I have some input i am getting from a client and posted to teh server via ajax.

A ample of such data is

Woodvale Grove, Nairobi, Kenya| Garissa| Mombasa Road

I am using php and i am then making this request with my key

$pickup = $this->input->post('pick');
$destination = $this->input->post('dest');
$user_id = $this->input->post('user_id');
$additional_destinations = $this->input->post('more_dests');


echo $additional_destinations;

$json = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=".$pickup."&destinations=".$additional_destinations."&mode=driving&key=hidden");
        $distance_arr = json_decode($json);
        echo $distance_arr;

I get an error 400 and this is the error

Severity: Warning

Message: file_get_contents(https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=Duma Road, Nairobi, Kenya&destinations=Woodvale Grove, Nairobi, Kenya| Garissa| Mombasa Road&mode=driving&key=hidden): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request

I copied the link in the error and noticed that the key is not being recognised or read.

How should i fix this?.

Gandalf
  • 1
  • 29
  • 94
  • 165

2 Answers2

0

You need add Payment method for your API key to work. Did you add Payment method?

Yaser Basravi
  • 51
  • 1
  • 1
  • 7
0

Solved it this way

$distance_data = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?&units=metric&origins='.urlencode($pickup).'&destinations='.urlencode($additional_destinations).'&key=hidden');
$distance_arr = json_decode($distance_data);

print_r($distance_arr);
Gandalf
  • 1
  • 29
  • 94
  • 165