在线服务" 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);
}
答案 0 :(得分:0)
eTermin常见问题解答未提供所需的所有信息。要获得GET或POST,您需要在标题中发送一个公钥,一个盐和一个编码签名。
所以这是获取我的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@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 .....
现在我需要做的就是以某种方式格式化这个并使char编码起作用。
答案 1 :(得分: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;
}