Sinch SMS Api授权错误

时间:2017-04-12 05:57:22

标签: php api sms sinch

我试图使用Sinch Sms Api,但是我一直收到错误:签名无效,下面是我用来调用Api的PHP代码,有人可以帮我解决。谢谢。 是时间戳吗?我使用这行代码创建一个UTC时间戳$ utc_str = gmdate(" Y-m-d \ TH:i:s \ Z");

 <?php
 $key = "000000-1111-4335-b1c1-11111111";
 $secret = "0000000000000/rw==";
 $phone_number = "919920283453";
 $S_date = date('Y-m-d H:i:s');
 $datetime = date("Y-m-d H:i:s", time() - date("Z"));
 $message = "Hello";
 $from ="";


 $utc_str = gmdate("Y-m-d\TH:i:s\Z");


 $data = json_encode(array('Message'=>$message, ));

 $content_type          = "application/json; charset=UTF-8";
 $canonicalized_headers = "x-timestamp:".$utc_str ;
 $path                  = "/v1/sms/".$phone_number;



 $content_md5 = base64_encode( md5( utf8_encode($data), true ));


 $string_to_sign =
 "POST\n".
 $content_md5."\n".
 $content_type."\n".
 $canonicalized_headers."\n".
 $path;
 $generator = new SinchTicketGenerator($key, $secret);

 $signature = base64_encode(hash_hmac("sha256",  
 utf8_encode($string_to_sign), $secret, true));





 $authorization  = "application " . $key . ":" . $signature;

 $ch = curl_init('https://messagingapi.sinch.com/v1/sms/' . 
 $phone_number);
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('content-type: 
 application/json; charset=UTF-8',
                                           'x-timestamp:'  . 
 $utc_str,
                                           'authorization:' . 
 $authorization));



 $result = curl_exec($ch);


 if(curl_errno($ch)) {
    echo 'Curl error: '. curl_error($ch);
 } else {
    echo $result;
 }

 curl_close($ch);

 }

0 个答案:

没有答案