No Description

CreateTopicAndPublishMessage.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. require_once(dirname(dirname(dirname(__FILE__))).'/mns-autoloader.php');
  3. use AliyunMNS\Client;
  4. use AliyunMNS\Model\SubscriptionAttributes;
  5. use AliyunMNS\Requests\PublishMessageRequest;
  6. use AliyunMNS\Requests\CreateTopicRequest;
  7. use AliyunMNS\Exception\MnsException;
  8. class CreateTopicAndPublishMessage
  9. {
  10. private $ip;
  11. private $port;
  12. private $accessId;
  13. private $accessKey;
  14. private $endPoint;
  15. private $client;
  16. public function __construct($ip, $port, $accessId, $accessKey, $endPoint)
  17. {
  18. $this->ip = $ip;
  19. $this->port = strval($port);
  20. $this->accessId = $accessId;
  21. $this->accessKey = $accessKey;
  22. $this->endPoint = $endPoint;
  23. }
  24. public function run()
  25. {
  26. $topicName = "CreateTopicAndPublishMessageExample";
  27. $this->client = new Client($this->endPoint, $this->accessId, $this->accessKey);
  28. // 1. create topic
  29. $request = new CreateTopicRequest($topicName);
  30. try
  31. {
  32. $res = $this->client->createTopic($request);
  33. echo "TopicCreated! \n";
  34. }
  35. catch (MnsException $e)
  36. {
  37. echo "CreateTopicFailed: " . $e;
  38. return;
  39. }
  40. $topic = $this->client->getTopicRef($topicName);
  41. // 2. subscribe
  42. $subscriptionName = "SubscriptionExample";
  43. $attributes = new SubscriptionAttributes($subscriptionName, 'http://' . $this->ip . ':' . $this->port);
  44. try
  45. {
  46. $topic->subscribe($attributes);
  47. echo "Subscribed! \n";
  48. }
  49. catch (MnsException $e)
  50. {
  51. echo "SubscribeFailed: " . $e;
  52. return;
  53. }
  54. // 3. send message
  55. $messageBody = "test";
  56. // as the messageBody will be automatically encoded
  57. // the MD5 is calculated for the encoded body
  58. $bodyMD5 = md5(base64_encode($messageBody));
  59. $request = new PublishMessageRequest($messageBody);
  60. try
  61. {
  62. $res = $topic->publishMessage($request);
  63. echo "MessagePublished! \n";
  64. }
  65. catch (MnsException $e)
  66. {
  67. echo "PublishMessage Failed: " . $e;
  68. return;
  69. }
  70. // 4. sleep for receiving notification
  71. sleep(20);
  72. // 5. unsubscribe
  73. try
  74. {
  75. $topic->unsubscribe($subscriptionName);
  76. echo "Unsubscribe Succeed! \n";
  77. }
  78. catch (MnsException $e)
  79. {
  80. echo "Unsubscribe Failed: " . $e;
  81. return;
  82. }
  83. // 6. delete topic
  84. try
  85. {
  86. $this->client->deleteTopic($topicName);
  87. echo "DeleteTopic Succeed! \n";
  88. }
  89. catch (MnsException $e)
  90. {
  91. echo "DeleteTopic Failed: " . $e;
  92. return;
  93. }
  94. }
  95. }
  96. $accessId = "";
  97. $accessKey = "";
  98. $endPoint = "";
  99. $ip = ""; //公网IP
  100. $port = "8000";
  101. if (empty($accessId) || empty($accessKey) || empty($endPoint))
  102. {
  103. echo "Must Provide AccessId/AccessKey/EndPoint to Run the Example. \n";
  104. return;
  105. }
  106. $instance = new CreateTopicAndPublishMessage($ip, $port, $accessId, $accessKey, $endPoint);
  107. $instance->run();
  108. ?>