How to consume here.com's REST api service

时间:2015-07-28 16:49:02

标签: javascript php rest curl soap

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);

1 个答案:

答案 0 :(得分:0)

我最终发现了错误..这是使用我发布的here.com REST api的一个工作示例,以便处于相同情况的任何人都可以查看它。问题是查询字符串的构造不正确,因为我刚从浏览器复制并粘贴它。如果有人遇到同样的问题,我建议您查看文档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;?>'>