1

i need to calculate the distance between two area of UK. That's why i use Google Distance Matrix API. In my local host server its works fine, but when i upload it in the internet, its not working. I already get API key from google, verify my website, create a project and add that key in the google. nothing is working for me. Please check my code.

    $postcode1=$this->input->post('postcode1');
    $postcode2=$this->input->post('postcode2');
    $result = array();
    $url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=$postcode1&destinations=$postcode2&mode=driving&language=en-EN&sensor=false&key=MY_API_KEY";
    $data = @file_get_contents($url);
    $result = json_decode($data, true);
    $distance=$result['rows'][0]['elements'][0]['distance']['text'];
    echo $distance;
Sumon
  • 301
  • 2
  • 4
  • 11
  • is there errors in the browser console or any errors on the google console page for the api? you can view any errors. – mushcraft Dec 19 '17 at 14:11
  • no error is showing – Sumon Dec 21 '17 at 19:00
  • is there a error if you unsurpress the file_get_contents? Can I check that you're $postcode1 and $postcode2 in the string is being concatinated to the $url or wrapped with {} and you've just shortformed it for your question? – mushcraft Jan 03 '18 at 12:41
  • There should also be a status element in the root of result and a status element for the current 'elements' index. – mushcraft Jan 03 '18 at 12:53

0 Answers0