No Description

CreateQueueAndSendMessage.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. require_once(dirname(dirname(dirname(__FILE__))).'/mns-autoloader.php');
  3. use AliyunMNS\Client;
  4. use AliyunMNS\Requests\SendMessageRequest;
  5. use AliyunMNS\Requests\CreateQueueRequest;
  6. use AliyunMNS\Exception\MnsException;
  7. class CreateQueueAndSendMessage
  8. {
  9. private $accessId;
  10. private $accessKey;
  11. private $endPoint;
  12. private $client;
  13. public function __construct($accessId, $accessKey, $endPoint)
  14. {
  15. $this->accessId = $accessId;
  16. $this->accessKey = $accessKey;
  17. $this->endPoint = $endPoint;
  18. }
  19. public function run()
  20. {
  21. $queueName = "CreateQueueAndSendMessageExample";
  22. $this->client = new Client($this->endPoint, $this->accessId, $this->accessKey);
  23. // 1. create queue
  24. $request = new CreateQueueRequest($queueName);
  25. try
  26. {
  27. $res = $this->client->createQueue($request);
  28. echo "QueueCreated! \n";
  29. }
  30. catch (MnsException $e)
  31. {
  32. echo "CreateQueueFailed: " . $e;
  33. return;
  34. }
  35. $queue = $this->client->getQueueRef($queueName);
  36. // 2. send message
  37. $messageBody = "test";
  38. // as the messageBody will be automatically encoded
  39. // the MD5 is calculated for the encoded body
  40. $bodyMD5 = md5(base64_encode($messageBody));
  41. $request = new SendMessageRequest($messageBody);
  42. try
  43. {
  44. $res = $queue->sendMessage($request);
  45. echo "MessageSent! \n";
  46. }
  47. catch (MnsException $e)
  48. {
  49. echo "SendMessage Failed: " . $e;
  50. return;
  51. }
  52. // 3. receive message
  53. $receiptHandle = NULL;
  54. try
  55. {
  56. // when receiving messages, it's always a good practice to set the waitSeconds to be 30.
  57. // it means to send one http-long-polling request which lasts 30 seconds at most.
  58. $res = $queue->receiveMessage(30);
  59. echo "ReceiveMessage Succeed! \n";
  60. if (strtoupper($bodyMD5) == $res->getMessageBodyMD5())
  61. {
  62. echo "You got the message sent by yourself! \n";
  63. }
  64. $receiptHandle = $res->getReceiptHandle();
  65. }
  66. catch (MnsException $e)
  67. {
  68. echo "ReceiveMessage Failed: " . $e;
  69. return;
  70. }
  71. // 4. delete message
  72. try
  73. {
  74. $res = $queue->deleteMessage($receiptHandle);
  75. echo "DeleteMessage Succeed! \n";
  76. }
  77. catch (MnsException $e)
  78. {
  79. echo "DeleteMessage Failed: " . $e;
  80. return;
  81. }
  82. // 5. delete queue
  83. try {
  84. $this->client->deleteQueue($queueName);
  85. echo "DeleteQueue Succeed! \n";
  86. } catch (MnsException $e) {
  87. echo "DeleteQueue Failed: " . $e;
  88. return;
  89. }
  90. }
  91. }
  92. $accessId = "";
  93. $accessKey = "";
  94. $endPoint = "";
  95. if (empty($accessId) || empty($accessKey) || empty($endPoint))
  96. {
  97. echo "Must Provide AccessId/AccessKey/EndPoint to Run the Example. \n";
  98. return;
  99. }
  100. $instance = new CreateQueueAndSendMessage($accessId, $accessKey, $endPoint);
  101. $instance->run();
  102. ?>