开发者社区> 问答> 正文

短信消息API---PHP



简介


当您使用短信的API接口发送短信后,可以通过使用MNS的Queue模型来接收短信的回执消息,假如服务出现异常情况时(如网络问题),导致消息回执未成功获取,还可以通过短信发送状态查询API接口进行一定的补偿(目前支持30天内发送记录的查询)。

消息的订阅

  • 云通信的所有业务消息都用过MNS消息服务向外发送。用户每订阅一个类别的消息(比如上行短信消息SmsUp),系统都会为用户分配一个独立的消息队列。
  • 用户可以通过阿里云账号拿到一个临时的token用于获取队列中的消息。用户可以下载demo,编写简单的消息处理类即可完成消息处理的任务。
  • 在页面上订阅消息,订阅完消息后,能拿到消息队列名称(queueName)。比如:Alicom-Queue-xxxxxx-SmsReport 。队列名字每个用户都不同。


消息类型


短信提供2种消息类型SmsReport(短信回执报告消息) 和 SmsUp(上行短信消息)
  • 通过订阅SmsReport短信状态报告,可以获知每条短信的发送情况,了解短信是否达到终端用户的状态与相关信息
  • 通过订阅SmsUp上行短信消息,可以获知终端用户回复短信的内容


短信回执消息SmsReport消息体格式

名称类型描述示例是否必须
phone_numberString短信接收号码13000000000可选
successBoolean发送是否成功true必须
biz_idString发送回执ID1234^345必须
out_idString调用发送短信接口时传的outId123456可选
send_timeString转发给运营商的时间2017-06-01 10:00:00必须
report_timeString收到运营商回执的时间2017-06-01 10:00:05可选
err_codeString错误码UNKNOW可选
err_msgString错误信息未知异常可选
sms_sizeString140字节算一条短信,短信长度超过140字节时会拆分成多条短信发送1,2,3可选


上行短信消息SmsUp

名称类型描述示例是否必须
phone_numberString短信接收号码13000000000可选
contentString短信内容true必须
send_timeString时间20150101120000必须
dest_codeString扩展码123456必须
sequence_idString消息序列ID123456必须


技术对接步骤



下载消息SDK


下载对应短信的消息DEMO工程,工程所需要的所有依赖php包都放在DEMO工程的目录下,将对于的php包引入到您的工程当中既可按照DEMO样例编写接收消息的程序。
SDK&DEMO[ 下载地址]

编写样例程序

<pre style='background: rgb(246, 246, 246); font: 12px/1.6 "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; padding: 10px; outline: 0px; border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; overflow: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px !important; margin-left: 0px; word-spacing: 0px; white-space: pre-wrap; word-wrap: break-word; box-sizing: border-box; orphans: 2; widows: 2; font-size-adjust: none; font-stretch: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' prettyprinted?="" linenums="">
  1. class ReceiveAlicomMsgDemo {
  2.     public function dealMessage($message) {
  3.         echo $message;
  4.         //TODO 这里开始写业务代码
  5.         return true;//返回true,则工具类自动删除已拉取的消息。返回false,消息不删除可以下次获取
  6.     }
  7.     public function receiveMsg(){        
  8.         //此处需要替换成开发者自己的AK
  9.         $accessKeyId = "your_accessKeyId";
  10.         $accessKeySecret = "your_accessKeySecret";
  11.         $messageType = "SmsReport";//短信回执:SmsReport,短息上行:SmsUp,语音呼叫:VoiceReport,流量直冲:FlowReport
  12.         $queueName = "your_queueName"; //在云通信页面开通相应业务消息后,就能在页面上获得对应的queueName
  13.         DefaultProfile::addEndpoint("cn-hangzhou","cn-hangzhou","Dybaseapi","dybaseapi.aliyuncs.com");
  14.         $tokenGetterForAlicom = new TokenGetterForAlicom($accessKeyID,$accessKeySecret);
  15.         $i = 0;
  16.         while ( $i <= 3) {//取回执消息失败5 次停止循环拉取
  17.             $i++;
  18.             try
  19.             {
  20.                 $tokenForAlicom = $tokenGetterForAlicom->getToeknByMessageType($messageType,$queueName);    
  21.                 $queue = $tokenForAlicom->getClient()->getQueueRef($queueName);
  22.                 // 3. receive message
  23.                 $receiptHandle = NULL;
  24.                 // when receiving messages, it's always a good practice to set the waitSeconds to be 30.
  25.                 // it means to send one http-long-polling request which lasts 30 seconds at most.
  26.                 $res = $queue->receiveMessage(2);
  27.                 echo "ReceiveMessage Succeed! \n";
  28.                 $bodyMD5 = md5(base64_encode($res->getMessageBody()));
  29.                 $receiptHandle = $res->getReceiptHandle();
  30.                 if (strtoupper($bodyMD5) == $res->getMessageBodyMD5())
  31.                 {
  32.                     if($this->dealMessage($res->getMessageBody())){
  33.                         $res = $queue->deleteMessage($receiptHandle);
  34.                         echo "DeleteMessage Succeed! \n";
  35.                     }
  36.                 }
  37.             }
  38.             catch (MnsException $e)
  39.             {
  40.                 echo "ex:".($e->getMnsErrorCode()) ;
  41.                 echo "ReceiveMessage Failed: " . $e;
  42.                 echo "<br>";
  43.                 #return;
  44.             }
  45.         }
  46.     }
  47. }
  48. $demo = new  ReceiveAlicomMsgDemo();
  49. $demo->receiveMsg();
  50. echo "<br>end";

展开
收起
nicenelly 2017-10-25 11:41:55 2462 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
CUDA MATH API 立即下载
API PLAYBOOK 立即下载
传统企业的“+互联网”-API服务在京东方的实践 立即下载