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

问题描述 投票:-2回答:2

在线服务“eTermin”提供了一个API Url,用于输出我的服务评论。

我尝试使用此代码,但只有一个错误的请求和一个错误:

$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);
}
php json curl
2个回答
0
投票

eTermin常见问题解答未提供所需的所有信息。要获取GET或POST,您需要在Header中发送一个publickey,salt和一个编码签名。

所以这是获取我的eTermin帐户评级的解决方案(退货尚未格式化!):

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

这是它返回的内容:

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 protected], ) [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 .....

现在我需要做的就是以某种方式格式化这个并使char编码工作。


0
投票

为了解析eTermin评级的JSON,请使用以下foreach循环:

//Traverse array __standard_Obj
    foreach ($decoded as $key => $value) {          
        $c_feeback = $value["CustomerFeedback"];
        $c_name = $value["CustomerInfo"];
        $c_name = before ('(',$c_name);
        $c_rating = $value["Rating"];
        $c_date = $value["RatingDate"];
        if(!empty($c_feeback)):
            echo '<h2>Feedback:'.$x.'</h2>';
            $x++;
            echo '<div>';
                if(strlen($c_name)>3):
                    echo $c_name;
                else:
                    echo "Anonym";
                endif;
            echo ': '.$c_feeback;
            //positiv stars
            //$c_rating = 4; //testvalue
            for($c = 0; $c < $c_rating; $c++) {
                echo '<span class="fa fa-star checked"></span>';
            }
            //negative stars
            $c_rating = 5 - $c_rating;
            for($c = 0; $c < $c_rating; $c++) {
                echo '<span class="fa fa-star"></span>';
            }
        endif;
    }
© www.soinside.com 2019 - 2024. All rights reserved.