Search code examples
phprestcodeignitercurlhere-api

I got some unorganized texts while requesting the route of two lat long through here api


i am using rest api v7 of here geocoding and search api and the url is https://route.ls.hereapi.com/routing/7.2/calculateroute.json?apiKey=[here api key]&waypoint0=geo!22.578765,88.295014&waypoint1=geo!22.570253,88.301865&routeattributes=wp,sm,sh,sc&mode=fastest;car but when i try with postman and with nomal search with google chrome then it give me the expected json return but when i am trying to get this with a curl request through codeigniter it give me first some unorganized texts and then it return a response json. so i need to get the distance from the json. but because of the unorganized data coming first i can not track it...... here is my code which i am using in my codeigniter.

function shipcalculate(){
  $curl = curl_init();

  curl_setopt_array($curl, array(
    CURLOPT_URL => "https://route.ls.hereapi.com/routing/7.2/calculateroute.json?apiKey=[here api key]&waypoint0=geo!22.578765,88.295014&waypoint1=geo!22.570253,88.301865&routeattributes=wp,sm,sh,sc&mode=fastest;car",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
  ));

  $response = curl_exec($curl);

  curl_close($curl);
  echo $response;
  //print_r(json_decode(json_encode($response)));
  //print_r(json_decode($response)) ;

}

the ouptut is coming as

enter image description here

but i want to get the output as a standerd json format.

please help me.......


Solution

  • ok i got it....

          $newresponse = json_decode($response);
          $array = json_decode(json_encode($newresponse), true);
          echo $array['response']['route'][0]['summary']['distance'];
    

    I add these three lines after curl close, and I got the distance. also, I understand now why the texts are coming, the texts are coming because of there is HTML in the JSON response.