流量回执消息如何查询?-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

流量回执消息如何查询?

猫饭先生 2017-10-25 13:57:04 1443


简介


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

消息的订阅


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

消息类型


流量提供一种消息类型,流量充值回执消息FlowReport

流量充值回执消息FlowReport消息体格式

名称类型描述示例是否必须
phoneString充值的手机号13000000000必须
out_idString外部扩展ID,在充值接口调用时指定123必须
resultString充值结果3-充值成功,4-充值失败必须
err_codeString错误码UNKNOW可选
err_msgString错误信息描述未知错误可选


技术对接步骤



下载消息SDK


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

编写样例程序


class ReceiveAlicomMsgDemo { public function dealMessage($message) {
  1. [backcolor=transparent]    echo $message[backcolor=transparent];
  2. [backcolor=transparent]    [backcolor=transparent]//TODO 这里开始写业务代码
  3. [backcolor=transparent]    [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent]true[backcolor=transparent];[backcolor=transparent]//返回true,则工具类自动删除已拉取的消息。返回false,消息不删除可以下次获取
  4. [backcolor=transparent]}
  5. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]function[backcolor=transparent] receiveMsg[backcolor=transparent](){[backcolor=transparent]        
  6. [backcolor=transparent]    $accessId [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"your_accessKey"[backcolor=transparent];
  7. [backcolor=transparent]    $accessKey [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"your_accessSecret"[backcolor=transparent];
  8. [backcolor=transparent]    $messageType [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"FlowReport"[backcolor=transparent];[backcolor=transparent]//短信回执:SmsReport,短息上行:SmsUp,语音呼叫:VoiceReport,流量直冲:FlowReport
  9. [backcolor=transparent]    $queueName [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"your_queueName"[backcolor=transparent];[backcolor=transparent] [backcolor=transparent]//在云通信页面开通相应业务消息后,就能在页面上获得对应的queueName
  10. [backcolor=transparent]    [backcolor=transparent]DefaultProfile[backcolor=transparent]::[backcolor=transparent]addEndpoint[backcolor=transparent]([backcolor=transparent]"cn-hangzhou"[backcolor=transparent],[backcolor=transparent]"cn-hangzhou"[backcolor=transparent],[backcolor=transparent]"Dybaseapi"[backcolor=transparent],[backcolor=transparent]"dybaseapi.aliyuncs.com"[backcolor=transparent]);
  11. [backcolor=transparent]    $tokenGetterForAlicom [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]TokenGetterForAlicom[backcolor=transparent]([backcolor=transparent]$accessKeyID[backcolor=transparent],[backcolor=transparent]$accessKeySecret[backcolor=transparent]);
  12. [backcolor=transparent]    $i [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]0[backcolor=transparent];[backcolor=transparent]
  13. [backcolor=transparent]    [backcolor=transparent]while[backcolor=transparent] [backcolor=transparent]([backcolor=transparent] $i [backcolor=transparent]<=[backcolor=transparent] [backcolor=transparent]3[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{[backcolor=transparent]//取回执消息失败5 次停止循环拉取
  14. [backcolor=transparent]        $i[backcolor=transparent]++;
  15. [backcolor=transparent]        [backcolor=transparent]try
  16. [backcolor=transparent]        [backcolor=transparent]{
  17. [backcolor=transparent]            $tokenForAlicom [backcolor=transparent]=[backcolor=transparent] $tokenGetterForAlicom[backcolor=transparent]->[backcolor=transparent]getToeknByMessageType[backcolor=transparent]([backcolor=transparent]$messageType[backcolor=transparent],[backcolor=transparent]$queueName[backcolor=transparent]);[backcolor=transparent]    
  18. [backcolor=transparent]            $queue [backcolor=transparent]=[backcolor=transparent] $tokenForAlicom[backcolor=transparent]->[backcolor=transparent]getClient[backcolor=transparent]()->[backcolor=transparent]getQueueRef[backcolor=transparent]([backcolor=transparent]$queueName[backcolor=transparent]);
  19. [backcolor=transparent]            [backcolor=transparent]// 3. receive message
  20. [backcolor=transparent]            $receiptHandle [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  21. [backcolor=transparent]            [backcolor=transparent]// when receiving messages, it's always a good practice to set the waitSeconds to be 30.
  22. [backcolor=transparent]            [backcolor=transparent]// it means to send one http-long-polling request which lasts 30 seconds at most.
  23. [backcolor=transparent]            $res [backcolor=transparent]=[backcolor=transparent] $queue[backcolor=transparent]->[backcolor=transparent]receiveMessage[backcolor=transparent]([backcolor=transparent]2[backcolor=transparent]);
  24. [backcolor=transparent]            echo [backcolor=transparent]"ReceiveMessage Succeed! \n"[backcolor=transparent];
  25. [backcolor=transparent]            $bodyMD5 [backcolor=transparent]=[backcolor=transparent] md5[backcolor=transparent]([backcolor=transparent]base64_encode[backcolor=transparent]([backcolor=transparent]$res[backcolor=transparent]->[backcolor=transparent]getMessageBody[backcolor=transparent]()));
  26. [backcolor=transparent]            $receiptHandle [backcolor=transparent]=[backcolor=transparent] $res[backcolor=transparent]->[backcolor=transparent]getReceiptHandle[backcolor=transparent]();
  27. [backcolor=transparent]            [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]strtoupper[backcolor=transparent]([backcolor=transparent]$bodyMD5[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]==[backcolor=transparent] $res[backcolor=transparent]->[backcolor=transparent]getMessageBodyMD5[backcolor=transparent]())
  28. [backcolor=transparent]            [backcolor=transparent]{
  29. [backcolor=transparent]                [backcolor=transparent]if[backcolor=transparent]([backcolor=transparent]$this[backcolor=transparent]->[backcolor=transparent]dealMessage[backcolor=transparent]([backcolor=transparent]$res[backcolor=transparent]->[backcolor=transparent]getMessageBody[backcolor=transparent]())){
  30. [backcolor=transparent]                    $res [backcolor=transparent]=[backcolor=transparent] $queue[backcolor=transparent]->[backcolor=transparent]deleteMessage[backcolor=transparent]([backcolor=transparent]$receiptHandle[backcolor=transparent]);
  31. [backcolor=transparent]                    echo [backcolor=transparent]"DeleteMessage Succeed! \n"[backcolor=transparent];
  32. [backcolor=transparent]                [backcolor=transparent]}
  33. [backcolor=transparent]            [backcolor=transparent]}
  34. [backcolor=transparent]        [backcolor=transparent]}
  35. [backcolor=transparent]        [backcolor=transparent]catch[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]MnsException[backcolor=transparent] $e[backcolor=transparent])
  36. [backcolor=transparent]        [backcolor=transparent]{
  37. [backcolor=transparent]            echo [backcolor=transparent]"ex:"[backcolor=transparent].([backcolor=transparent]$e[backcolor=transparent]->[backcolor=transparent]getMnsErrorCode[backcolor=transparent]())[backcolor=transparent] [backcolor=transparent];
  38. [backcolor=transparent]            echo [backcolor=transparent]"ReceiveMessage Failed: "[backcolor=transparent] [backcolor=transparent].[backcolor=transparent] $e[backcolor=transparent];
  39. [backcolor=transparent]            echo [backcolor=transparent]"<br>"[backcolor=transparent];
  40. [backcolor=transparent]            [backcolor=transparent]#return;
  41. [backcolor=transparent]        [backcolor=transparent]}
  42. [backcolor=transparent]    [backcolor=transparent]}
  43. [backcolor=transparent]}

}
$demo = new ReceiveAlicomMsgDemo();$demo->receiveMsg();echo “end”;?>
消息中间件 API PHP 开发工具
分享到
取消 提交回答
全部回答(0)
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题