简体   繁体   中英

How to consume here.com's REST api service

Has anyone tried using here.com's rest api? I'm trying to run an example request, but nothing happens. I'm new to REST, so I'm clueless about the problem. If the $url is plugged directly into the browsers address text box, the image is displayed, but my curl function in the code below does not retrieve anything, or so it seems.

$url = "http://image.maps.cit.api.here.com/mia/1.6/route?app_id=redacted&app_code=redacted&r0=52.5338,13.2966,52.538361,13.325329&r1=52.540867,13.262444,52.536691,13.264561,52.529172,13.268337,52.528337,13.273144,52.52583,13.27898,52.518728,13.279667&m0=52.5338,13.2966,52.538361,13.325329&m1=52.540867,13.262444,52.518728,13.279667&lc0=440000ff&sc0=440000ff&lw0=6&lc1=44ff00ff&sc1=44ff00ff&lw1=3";

$ch = curl_init($url);
$options = array(
            CURLOPT_CONNECTTIMEOUT => 20 ,
            CURLOPT_AUTOREFERER => true,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_RETURNTRANSFER => true,
           );

curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$result = json_decode($response);
echo "response: <br>";
echo "<br>";
echo "result: <br> " . $result;
curl_close($ch);

I found the error in the end.. This is a working sample of using the here.com REST api that I'm posting so that anyone who is in the same situation may have a look at it. The problem was that the construction of the query string was incorrect as I just copied and pasted it from the browser. If anybody comes across the same problem, I recommend taking a look at the documentation here ..

$base_url = 'http://image.maps.cit.api.here.com'; 
$path = '/mia/1.6/';
$resource = 'route';    // type of service... this should change if you want something else

$query_string = array(
    'app_id'    => 'XXXXXXXXXXXXXXXXXXXXXX',  //your app id
    'app_code'  => 'XXXXXXXXXXXXXXXXXXXXXX',  // your app code
    'r0'        => '52.540867,13.262444,52.536691,13.264561,52.529172,13.268337,52.528337,13.273144,52.52583,13.27898,52.518728,13.279667', // coords for the route
    'lw'        => '4',   // line width
    'lc'        => '00ff00',  // line color
);

$url = $base_url . $path.$resource . '?' . http_build_query($query_string);
$img_file = './img/test_img2.jpg';  // save image here

$ch = curl_init($url);
$fp = fopen($img_file, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

?>

<img src='<?php echo $img_file;?>'>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM