Reading list Switch to dark mode

    PHP get distance between two places using google maps

    Updated 16 July 2021

    Use curl to use google map api to get the distance.
    Here the format of address
    $address = $value["street"]." ".$value["region"]." ".$value["country_id"]." ".$value["zip_code"];

    Now just paste the code

    $url = "".str_replace(' ', '+', $source_address)."&destination=".str_replace(' ', '+', $destination_address)."&sensor=false";
    	            $ch = curl_init();
    	            curl_setopt($ch, CURLOPT_URL, $url);
    	            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	            curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
    	            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    	            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    	            $response = curl_exec($ch);
    	            $response_all = json_decode($response);
    	            // print_r($response);
    	            $distance = $response_all->routes[0]->legs[0]->distance->text;

    Get $distance and use

    Searching for an experienced
    Magento Company ?
    Read More
    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


  • Trevin Boucher
    I am getting an error: Notice: Undefined offset: 0

    It is referring to this line…$distance = $response_all->routes[0]->legs[0]->distance->text;

    • Trevin Boucher
      I left out some information…I am running this multiple times on the same page. It usually works perfectly fine, but I do get this error randomly. Often times after a couple refreshes it fixes itself.

      Does that make sense?

  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home