edit-icon download-icon

Receive notification through queues

Last Updated: Feb 09, 2018

Back to Overview

This section briefly introduces the requirements and installation instructions of MNS. For details, see the MNS documentation SDK download and Queue user manual.

The example language is PHP. For details about the usage instructions of other languages, see the MNS documentation SDK user manual.

  • Environment requirements

    PHP 5.5+

  • Installation

    Download the MNS SDK for PHP from Alibaba Cloud.

    Decompress the file to the project directory. The decompressed directory is php_sdk.

  • Sample code

    • Reference the MNS SDK

      1. require_once(dirname(__FILE__).'/php_sdk/mns-autoloader.php');
    • Initialize MNS

      MNS configures an independent service domain name for each region of users. The rule is https://${UserId}.mns.${Region}.aliyuncs.com. China East 1 (Hangzhou) (cn-hangzhou) is used in the following example. You can also use another region, for example, China North 2 (Beijing) (cn-beijing).

      1. use AliyunMNS\Client;
      2. use AliyunMNS\Exception\MnsException;
      1. $mns_client = new Client('https://'.$user_id.'.mns.cn-hangzhou.aliyuncs.com',
      2. $access_key_id, $access_key_secret);
      3. $queue = $mns_client->getQueueRef($queue_name);
    • Receive a message

      Each message received by MNS corresponds to a handle, which can be used later to operate the message (for example, delete the message).

      In addition, MNS supports receiving messages in batches to improve the performance. For details, see the MNS documentation BatchReceiveMessage.

      A timeout time can be specified when a message is received. (The timeout time is set to 3s in the following example.) If no message exists in the queue, timeout occurs and an exception is returned.

      1. $receipt_handle = NULL;
      2. $message = null;
      3. try
      4. {
      5. $res = $queue->receiveMessage(3);
      6. echo "ReceiveMessage Succeed! \n";
      7. $message = $res->getMessageBody();
      8. $receipt_handle = $res->getreceiptHandle();
      9. }
      10. catch (MnsException $e)
      11. {
      12. echo "ReceiveMessage Failed: " . $e . "\n";
      13. }
    • Delete a message

      A message is not actively deleted from a queue. You must call DeleteMessage to delete the message. Otherwise, the message is always in the queue, and you will receive the same message next time. In addition, DeleteMessage can be called successfully only within the specified time after the message is received. For details, see MNS - DeleteMessage.

      1. try
      2. {
      3. $res = $queue->deleteMessage($receipt_handle);
      4. echo "DeleteMessage Succeed! \n";
      5. }
      6. catch (MnsException $e)
      7. {
      8. echo "DeleteMessage Failed: " . $e . "\n";
      9. }
    • Analyze a message

      The message body is a string while the content is a JSON object. After converting the string to the object using json_decode, you can analyze the JSON object to obtain details of the message. The output file that triggers media workflow execution is printed in the following example.

      1. $json_message = json_decode($message);
      2. $input_file = $json_message->{'MediaWorkflowExecution'}->{'Input'}->{'InputFile'};
      3. echo 'input file location:'.$input_file->{'Location'}.
      4. ' bucket:'.$input_file->{'Bucket'}.
      5. ' object:'.$input_file->{'Object'}."\n";
    • Obtain video output details

      After obtaining details of a message, you can use the media library API to obtain details of a video executed by a workflow. The output URL of the transcoding and screenshot tasks is printed in the following example.

      For details about how to install and configure the SDK for PHP of the media library, see Media Library SDK-PHP.

      1. include_once 'aliyun-php-sdk-core/Config.php';
      2. use Mts\Request\V20140618 as Mts;

      Initialize the client of the media library.

      1. $profile = DefaultProfile::getProfile('cn-hangzhou',
      2. $access_key_id,
      3. $access_key_secret);
      4. $mts_client = new DefaultAcsClient($profile);

      Print the output URLs and basic information of all transcoding tasks.

      1. if (strcmp($json_message->{'Type'}, 'Report') == 0) {
      2. $activities = $json_message->{'MediaWorkflowExecution'}->{'ActivityList'};
      3. $transcode_job_ids = Array();
      4. for ($i=0; $i < count($activities); $i++) {
      5. if (strcmp($activities[$i]->{'Type'}, 'Transcode') == 0) {
      6. $transcode_job_ids[] = $activities[$i]->{'JobId'};
      7. }
      8. }
      9. $request = new Mts\QueryJobListRequest();
      10. $request->setJobIds(join(',', $transcode_job_ids));
      11. $request->setRegionId('cn-hangzhou');
      12. $response = $mts_client->getAcsResponse($request);
      13. for ($i=0; $i < count($response->{'JobList'}->{'Job'}); $i++) {
      14. $output = $response->{'JobList'}->{'Job'}[$i]->{'Output'};
      15. $output_file = $response->{'JobList'}->{'Job'}[$i]->{'Output'}->{'OutputFile'};
      16. $video_properties = $response->{'JobList'}->{'Job'}[$i]->{'Output'}->{'Properties'};
      17. echo 'URLs of the transcoding output files '.'http://'.$output_file->{'Bucket'}.'.'.
      18. $output_file->{'Location'}.'.aliyuncs.com/'.
      19. urldecode($output_file->{'Object'})."\n";
      20. echo 'basic information of the transcoding output files '.$video_properties->{'Width'}.'x'.$video_properties->{'Height'}.
      21. ' duration:'.$video_properties->{'Duration'}."\n";
      22. }
      23. }

      Print the output URLs of all screenshot tasks.

      1. if (strcmp($json_message->{'Type'}, 'Report') == 0) {
      2. $activities = $json_message->{'MediaWorkflowExecution'}->{'ActivityList'};
      3. $snapshot_job_ids = Array();
      4. for ($i=0; $i < count($activities); $i++) {
      5. if (strcmp($activities[$i]->{'Type'}, 'Snapshot') == 0) {
      6. $snapshot_job_ids[] = $activities[$i]->{'JobId'};
      7. }
      8. }
      9. $request = new Mts\QuerySnapshotJobListRequest();
      10. $request->setSnapshotJobIds(join(',', $snapshot_job_ids));
      11. $request->setRegionId('cn-hangzhou');
      12. $response = $mts_client->getAcsResponse($request);
      13. for ($i=0; $i < count($response->{'SnapshotJobList'}->{'SnapshotJob'}); $i++) {
      14. $snapshot_config = $response->{'SnapshotJobList'}->{'SnapshotJob'}[$i]->{'SnapshotConfig'};
      15. $output_file = $response->{'SnapshotJobList'}->{'SnapshotJob'}[$i]->{'SnapshotConfig'}->{'OutputFile'};
      16. echo 'URLs of the screenshot output files '.'http://'.$output_file->{'Bucket'}.'.'.
      17. $output_file->{'Location'}.'.aliyuncs.com/'.
      18. urldecode($output_file->{'Object'})."\n";
      19. }
      20. }
Thank you! We've received your feedback.