123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- require_once(dirname(dirname(dirname(__FILE__))).'/mns-autoloader.php');
- use AliyunMNS\Client;
- use AliyunMNS\Model\SubscriptionAttributes;
- use AliyunMNS\Requests\PublishMessageRequest;
- use AliyunMNS\Requests\CreateTopicRequest;
- use AliyunMNS\Exception\MnsException;
- class CreateTopicAndPublishMessage
- {
- private $ip;
- private $port;
- private $accessId;
- private $accessKey;
- private $endPoint;
- private $client;
- public function __construct($ip, $port, $accessId, $accessKey, $endPoint)
- {
- $this->ip = $ip;
- $this->port = strval($port);
- $this->accessId = $accessId;
- $this->accessKey = $accessKey;
- $this->endPoint = $endPoint;
- }
- public function run()
- {
- $topicName = "CreateTopicAndPublishMessageExample";
- $this->client = new Client($this->endPoint, $this->accessId, $this->accessKey);
- // 1. create topic
- $request = new CreateTopicRequest($topicName);
- try
- {
- $res = $this->client->createTopic($request);
- echo "TopicCreated! \n";
- }
- catch (MnsException $e)
- {
- echo "CreateTopicFailed: " . $e;
- return;
- }
- $topic = $this->client->getTopicRef($topicName);
- // 2. subscribe
- $subscriptionName = "SubscriptionExample";
- $attributes = new SubscriptionAttributes($subscriptionName, 'http://' . $this->ip . ':' . $this->port);
- try
- {
- $topic->subscribe($attributes);
- echo "Subscribed! \n";
- }
- catch (MnsException $e)
- {
- echo "SubscribeFailed: " . $e;
- return;
- }
- // 3. send message
- $messageBody = "test";
- // as the messageBody will be automatically encoded
- // the MD5 is calculated for the encoded body
- $bodyMD5 = md5(base64_encode($messageBody));
- $request = new PublishMessageRequest($messageBody);
- try
- {
- $res = $topic->publishMessage($request);
- echo "MessagePublished! \n";
- }
- catch (MnsException $e)
- {
- echo "PublishMessage Failed: " . $e;
- return;
- }
- // 4. sleep for receiving notification
- sleep(20);
- // 5. unsubscribe
- try
- {
- $topic->unsubscribe($subscriptionName);
- echo "Unsubscribe Succeed! \n";
- }
- catch (MnsException $e)
- {
- echo "Unsubscribe Failed: " . $e;
- return;
- }
- // 6. delete topic
- try
- {
- $this->client->deleteTopic($topicName);
- echo "DeleteTopic Succeed! \n";
- }
- catch (MnsException $e)
- {
- echo "DeleteTopic Failed: " . $e;
- return;
- }
- }
- }
- $accessId = "";
- $accessKey = "";
- $endPoint = "";
- $ip = ""; //公网IP
- $port = "8000";
- if (empty($accessId) || empty($accessKey) || empty($endPoint))
- {
- echo "Must Provide AccessId/AccessKey/EndPoint to Run the Example. \n";
- return;
- }
- $instance = new CreateTopicAndPublishMessage($ip, $port, $accessId, $accessKey, $endPoint);
- $instance->run();
- ?>
|