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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog