SMS API 示例代码(PHP)
高****师 · 高****师 发布于2015-12-07 18:43 浏览:5180 回复:12

通过该demo,基本上可以模拟写出开放云的所有API示例代码,拿走不谢。。。


date_default_timezone_set('UTC');

$AK = "AAAAAAAAAAAAAAAAAAAAA";

$timestamp = date("Y-m-d")."T".date("H:i:s")."Z";

$expirationPeriodInSeconds = "3600";

$SK="BBBBBBBBBBBBBBBBBBB";

   

  //$authStringPrefix = "bce-auth-v1/".$AK."/".$timestamp."/".$expirationPeriodInSeconds;

$authStringPrefix = "bce-auth-v1"."/".$AK."/".$timestamp."/".$expirationPeriodInSeconds;

$SigningKey=hash_hmac('SHA256',$authStringPrefix,$SK);

$CanonicalHeaders1 = "host;"."x-bce-date";

$CanonicalHeaders2 = "host:sms.bj.baidubce.com\n"."x-bce-date:".urlencode($timestamp);//

print "CanonicalHeaders2:".$CanonicalHeaders2."\n";


$CanonicalString = "";

$CanonicalURI = "/v1/message"; 

$CanonicalRequest = "POST\n".$CanonicalURI."\n".$CanonicalString."\n".$CanonicalHeaders2; //第二步

print "CanonicalRequest:".$CanonicalRequest."\n";

$Signature = hash_hmac('SHA256',$CanonicalRequest,$SigningKey);

print "Signature:".$Signature."\n";

$Authorization = "bce-auth-v1/{$AK}/".$timestamp."/{$expirationPeriodInSeconds}/{$CanonicalHeaders1}/{$Signature}";

//$Authorization="bce-auth-v1/AAAAAAAAAAAAAAAAAAAAAAAAAA/2015-08-28T05:03:28Z/3600/host;x-bce-date/722b421d5cd9973fbrrrbb3dfe5sfsf8af52a90f73e0ce28fb8f1c95c81483127c2";

print "Authorization:".$Authorization."\n";

$url = "http://sms.bj.baidubce.com/v1/message";

$data = array(

'templateId' => 'smsTpl:your template id',

'receiver' => array('18612345678'),

'contentVar' => json_encode(array(

'code' => '12345'

))

);

$data_string = json_encode($data);     

$head =  array("Content-Type:application/json","Authorization:{$Authorization}","x-bce-date:{$timestamp}","x-bce-content-sha256:{$SigningKey}");

 $curlp = curl_init();

 curl_setopt($curlp, CURLOPT_URL, $url);

 curl_setopt($curlp, CURLOPT_HTTPHEADER,$head); 

 curl_setopt($curlp, CURLOPT_POSTFIELDS, $data_string);

 if(!empty($data)){

curl_setopt($curlp, CURLOPT_POST, 1);                                                                    

 }

curl_setopt($curlp, CURLOPT_RETURNTRANSFER, 1);   

 $output = curl_exec($curlp);

 curl_close($curlp);

 echo  $output;

print "\n";

?>


点赞  ( 2 )
收藏
评论(12)
共12条回复 最后由通****路赞于2017-03-11 16:42
#2十****你回复于2015-12-08 10:24:32

                           总能看到你发的帖子,好人一生平安



                                                      版主为什么不加精,差评

1
#3高****师回复于2015-12-08 16:38:41
#2 十****你回复
[图片]总能看到你发的帖子,好人一生平安 版主为什么不加精,差评[图片]

看我这么辛苦,希望版主能给我申请几毛钱代金券

1
#4D****d回复于2015-12-10 17:16:27

应该开发一个道具,呼唤版主!

0
#5h****i回复于2015-12-16 14:25:22

以下两个 分别代表什么呀

AAAAAAAAAAAAAAAAAAAAAAA

   BBBBBBBBBBBBBBBBBBBBBB

0
#6高****师回复于2015-12-22 14:44:58
#5 h****i回复
以下两个 分别代表什么呀 AAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBBBB

在文档上有,就是每个百度开放云账号的AK、SK

0
#7d****n回复于2016-01-02 09:51:52

谢谢分享,一直没成功过,看了楼主的终于成功了。非常感谢。

0
#8b****v回复于2016-01-21 15:52:46

好人一生平安

0
#9b****3回复于2016-05-15 10:52:22

'contentVar' => json_encode(array(

 

是不是有漏

0
#10b****m回复于2016-07-03 12:56:52

我怎么测试不成功啊,提示模板内容非法

0
#11土****生回复于2016-08-08 23:20:18

真是太感谢了,搞了半天,不如老师轻轻一指点!

0
TOP