du548397507 2018-03-05 14:37
浏览 86
已采纳

Json数据来自API URL(使用curl)

The online service "eTermin" provides an API Url which outputs reviews of my service.

I tried it with this code but only got a bad request and an error:

$service_url = 'https://www.etermin.net/api/rating/';
$curl = curl_init($service_url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false); // DEBUGGING set to true
$curl_response = curl_exec($curl);
echo print_r($curl_response); // test the JSON array

if ($curl_response === false) {
    $info = curl_getinfo($curl);
    curl_close($curl);
    die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response, true);

if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
    die('error occured: ' . $decoded->response->errormessage);
}
  • 写回答

2条回答 默认 最新

  • douhu3424 2018-03-06 11:27
    关注

    The eTermin FAQ did not provide all the information needed. In order to GET or POST you need to send a publickey, a salt and an encoded signature in the Header.

    So this is the solution to GET the ratings of my eTermin account (the return is not formatted yet!):

    $publicKey = "[publicKey]";
    $secretKey = "[secretKey]";
    // Generates a random string of ten digits
    $salt = mt_rand();
    // Computes the signature by hashing the salt with the secret key as the key
    $signature = hash_hmac('sha256', $salt, $secretKey, true);
    // base64 encode
    $encodedSignature = base64_encode($signature);
    
    // CURL GET REQUEST
    $service_url = 'https://www.etermin.net/api/rating/';
    $curl = curl_init($service_url);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                        'publickey:'.$publicKey,
                        'salt:'.$salt,
                        'signature:'.$encodedSignature 
                    ));
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            //curl_setopt($curl, CURLOPT_HEADER, true);
    $curl_response = curl_exec($curl);
    $curl_response = json_decode($curl_response);
    echo print_r($curl_response);
    

    This is what it returns:

    Array ( [0] => stdClass Object ( [ID] => 60979 [AppointmentExternalID] => XXXXXXX [CustomerFeedback] => Sehr schöner Laden, sehr privat. Wir wurden von zwei Experten gleichzeitig beraten, Preise sind angemessen. Schöne Anzüge. Wir sind zufrieden. [Rating] => 5 [CustomerInfo] => Benjamin (email@something.at, ) [RatingDate] => 2018-01-24T17:21:20.793 [CalendarID] => 46499 [CalendarName] => Kalender [ServiceID] => 60347 [Publish] => 1 ) [1] => stdClass Object ( [ID] => 61014 [AppointmentExternalID] => XXXXXXXX [CustomerFeedback] => [Rating] => 5 .....
    

    Now all I need to do is format this somehow and get the char encoding to work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法