All Products
Search
Document Center

PHP

Last Updated: Nov 26, 2019

Input parameters

Name Type O/M Example Description
MessageType String Mandatory SmsReport SmsReport
QueueName String Mandatory Alicom-Queue-xxxxxx-SmsReport The name of the message queue returned from the console——System Setting.

Output parameters

Name Name Example Description
MessageId String 10080303003003 The ID generated by Alibaba Cloud to uniquely identify an SMS message.
To String 452220121 The phone number of the SMS message recipient.
Status Number 3 The status of the SMS message. Valid values:1 (Delivered), 2 (Failed), and 3 (Submitted).
ErrorCode String success The returned error code.
ErrorDescription String success The returned error message.
SendDate String Mon, 24 Dec 2018 16:58:22 +0800 The time and date when the SMS message was sent.
ReceiveDate String Mon, 24 Dec 2018 16:58:22 +0800 The time and date when the SMS message was received.

Developer integration process

1.SDK download: Click to view the download link

2.Writing a sample program

Need to rely on the “OpenAPI PHP Client ” and the “OpenAPI PHP SDK” .

OpenAPI PHP Client

Download address: openapi-sdk-php-client

OpenAPI PHP SDK

Download address: openapi-sdk-php

When calling the interface, please note:

  • The regionId must be “ap-southeast-1”, please do not modify it.

  • Product must be “Dybaseapi”, please do not modify it.

  • Version must be 2018-05-01, please do not modify.

  • Host must be “dybaseapi.ap-southeast-1.aliyuncs.com”, please do not modify.

  1. <?php
  2. use AlibabaCloud\Client\AlibabaCloud;
  3. use AlibabaCloud\Client\Exception\ClientException;
  4. use AlibabaCloud\Client\Exception\ServerException;
  5. use AlibabaCloud\Dybaseapi\MNS\Requests\BatchReceiveMessage;
  6. use AlibabaCloud\Dybaseapi\MNS\Requests\BatchDeleteMessage;
  7. AlibabaCloud::accessKeyClient('<AccessKeyId>', '<AccessSecret>')
  8. ->regionId('ap-southeast-1')
  9. ->asGlobalClient();
  10. // Message Quenue Name, you can get it from SMS console, like:Alicom-Queue-xxxxxx-SmsReport
  11. $queueName = '<QueueName>';
  12. // Message Type, smsReport
  13. $messageType = '<MessageType>';
  14. $response = null;
  15. $token = null;
  16. $i = 0;
  17. do {
  18. try {
  19. if (null == $token || strtotime($token['ExpireTime']) - time() > 2 * 60) {
  20. $response = AlibabaCloud::rpcRequest()
  21. ->product('Dybaseapi')
  22. ->version('2018-05-01')
  23. ->action('QueryTokenForMnsQueue')
  24. ->method('POST')
  25. ->host("sms-intl.ap-southeast-1.aliyuncs.com")
  26. ->options([
  27. 'query' => [
  28. 'MessageType' => $messageType,
  29. 'QueueName' => $queueName,
  30. ],
  31. ])
  32. ->request()
  33. ->toArray();
  34. }
  35. $token = $response['MessageTokenDTO'];
  36. $mnsClient = new \AlibabaCloud\Dybaseapi\MNS\MnsClient(
  37. "https://1493622401794734.mns.ap-southeast-1.aliyuncs.com",
  38. $token['AccessKeyId'],
  39. $token['AccessKeySecret'],
  40. $token['SecurityToken']
  41. );
  42. $mnsRequest = new BatchReceiveMessage(10, 5);
  43. $mnsRequest->setQueueName($queueName);
  44. $mnsResponse = $mnsClient->sendRequest($mnsRequest);
  45. $receiptHandles = Array();
  46. foreach ($mnsResponse->Message as $message) {
  47. // $receiptHandles[] = $message->ReceiptHandle;
  48. $messageBody = base64_decode($message->MessageBody);
  49. print_r($messageBody . "\n");
  50. }
  51. if (count($receiptHandles) > 0) {
  52. $deleteRequest = new BatchDeleteMessage($queueName, $receiptHandles);
  53. $mnsClient->sendRequest($deleteRequest);
  54. }
  55. } catch (ClientException $e) {
  56. echo $e->getErrorMessage() . PHP_EOL;
  57. } catch (ServerException $e) {
  58. if ($e->getCode() == 404) {
  59. $i++;
  60. }
  61. echo $e->getErrorMessage() . PHP_EOL;
  62. }
  63. } while ($i < 3);