我正在尝试在 Sinch 服务中发出conferenceCallout 请求,但我在签名方面遇到问题。我粘贴代码:
<?php
//JSon Object
$conferencia['method']="conferenceCallout";
$participante['cli']="46000000000";
$destination['type']="username";
$destination['endpoint']="roke1";
$participante['destination']=$destination;
$participante['domain']="mxp";
$participante['custom']="customData";
$participante['locale']="en-US";
$participante['greeting']="Welcome to my conference";
$participante['conferenceId']="conferencia_de_prueba";
$participante["enableDice"]=false;
$conferencia['conferenceCallout']=$participante;
$data=json_encode($conferencia);
$md5_body = base64_encode ( MD5 ( utf8_encode ( json_encode($conferencia) ) ) );
$applicationKey="XXXXXX-xXXX-XXXX-XXXX-XXXXXXXX";
$applicationSecret="XXXXXXXXXXXXXXXX==";
$timestamp = new DateTime('NOW');
$StringToSign ="POST
".$md5_body."
application/json
x-timestamp:".$timestamp->format(DateTime::ISO8601)."
/v1/callouts";
$utf8encode=utf8_encode($StringToSign);
$hash= hash_hmac("sha256",$applicationSecret,$utf8encode);
$base64=base64_encode($hash);
$Signature =$base64;
$Autorization = "Application"." ".$applicationKey.":".$Signature;
$ch = curl_init('https://callingapi.sinch.com/v1/callouts');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization:' . $Autorization,
'X-Timestamp: ' . $timestamp->format(DateTime::ISO8601),
'Content-Length: ' . strlen($data))
);
$result = curl_exec($ch);
echo $result;
?>
但我收到错误代码 40102,即 40102 - 签名无效。
谁能帮我?我做错了什么?
此致。
我发布了对我有用的代码。
$phone ="+460000000" //Put the destination number here
$key = "XXXXXX";
$secret = "XXXXXX";
$message = $first_name . ', thanks you for signing in. We will text you when we\'re ready for you';
$phone = $phone_number;
$body = json_encode(array('From' => $rented_number, 'Message'=>$message, ));
$timestamp = date("c");
$path = "/v1/sms".$phone;
$content_type = "application/json";
$canonicalized_headers = "x-timestamp:" . $timestamp;
$content_md5 = base64_encode( md5( utf8_encode($body), true ));
$string_to_sign =
"POST\n".
$content_md5."\n".
$content_type."\n".
$canonicalized_headers."\n".
$path;
$signature = base64_encode(hash_hmac("sha256", utf8_encode($string_to_sign), base64_decode($secret), true));
$authorization = "Application " . $key . ":" . $signature;
$service_url = 'https://messagingapi.sinch.com'.$path;
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'content-type: '.$content_type,
'x-timestamp:' . $timestamp,
'authorization:' . $authorization
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
// @todo: checking response / working with results
curl_close($curl);
感谢draw010 的帮助,太棒了!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)