tencent cloud

Feedback

SDK for PHP

Last updated: 2022-06-10 11:32:41

    Directions

    This document uses the SDK for PHP as an example to describe how to connect the client to TDMQ for CMQ and send/receive messages.

    Prerequisites

    Queue Model

    Directions

    1. Create a queue service in the console as instructed in Queue Management.
      Note

      You can create a message queue in the console or via TencentCloud API. To use TencentCloud API, you need to install the SDK 3.0 for PHP.

    2. Add dependencies.
      composer require tencentcloud/tencentcloud-sdk-php
    3. Add references.
      require '/path/to/vendor/autoload.php';
    4. Create a message queue.
      $cred = new Credential($secretId, $secretKey);
      $httpProfile = new HttpProfile();
      $httpProfile->setEndpoint($endPoint);

      $clientProfile = new ClientProfile();
      $clientProfile->setHttpProfile($httpProfile);
      $client = new TdmqClient($cred, "ap-guangzhou", $clientProfile);

      $req = new CreateCmqQueueRequest();

      $params = array(
      "QueueName" => "queue_api", // Message queue name
      // Below is the dead letter queue configuration
      "DeadLetterQueueName" => "dead_queue_api", // Name of the dead letter queue that needs to be created first
      "Policy" => 0, // Dead letter policy. 0: Message has been consumed multiple times but not deleted. 1: `Time-To-Live` has elapsed
      "MaxReceiveCount" => 3 // Maximum receipts. Value range: 1–1000
      // MaxTimeToLive: Maximum period in seconds before an unconsumed message expires, which is required if `policy` is 1. Value range: 300–43200. This value should be smaller than `msgRetentionSeconds` (maximum message retention period)
      );
      $req->fromJsonString(json_encode($params));

      $resp = $client->CreateCmqQueue($req);
      Parameter Description
      $endPoint API call address, which can be copied from Queue Service > API Request Address in the TDMQ for CMQ console. img
      $secretId, $secretKey TencentCloud API key, which can be copied on the Access Key > API Key Management page in the CAM console. img
    5. Import CMQ files into the project.
    6. Send messages.
      // Message queue name
      $queue_name = "php_queue";
      // Authentication information
      $my_account = new Account($this->endpoint, $this->secretId, $this->secretKey);

      $my_queue = $my_account->get_queue($queue_name);

      $queue_meta = new QueueMeta();
      $queue_meta->queueName = $queue_name;
      $queue_meta->pollingWaitSeconds = 10;
      $queue_meta->visibilityTimeout = 10;
      $queue_meta->maxMsgSize = 1024;
      $queue_meta->msgRetentionSeconds = 3600;

      try {
      // Message content
      $msg_body = "I am test message.";
      $msg = new Message($msg_body);
      // Send messages
      $re_msg = $my_queue->send_message($msg);
      echo "Send Message Succeed! MessageBody:" . $msg_body . " MessageID:" . $re_msg->msgId . "\n";
      } catch (CMQExceptionBase $e) {
      echo "Create Queue Fail! Exception: " . $e;
      return;
      }
      ParameterDescription
      endpointAPI call address, which can be copied from Queue Service > API Request Address in the TDMQ for CMQ console. img
      secretId, secretKeyTencentCloud API key, which can be copied on the Access Key > API Key Management page in the CAM console. img
      $queue_nameQueue name, which can be obtained on the Queue Service page in the TDMQ for CMQ console.
    7. Consume messages.
      // Message queue name
      $queue_name = "php_queue";
      // Authentication result
      $my_account = new Account($this->endpoint, $this->secretId, $this->secretKey);

      $my_queue = $my_account->get_queue($queue_name);

      $queue_meta = new QueueMeta();
      $queue_meta->queueName = $queue_name;
      $queue_meta->pollingWaitSeconds = 10;
      $queue_meta->visibilityTimeout = 10;
      $queue_meta->maxMsgSize = 1024;
      $queue_meta->msgRetentionSeconds = 3600;

      try {
      // Obtain messages
      $recv_msg = $my_queue->receive_message(3);
      echo "Receive Message Succeed! " . $recv_msg . "\n";
      // Successfully consumed messages are deleted
      $my_queue->delete_message($recv_msg->receiptHandle);
      } catch (CMQExceptionBase $e) {
      echo "Create Queue Fail! Exception: " . $e;
      return;
      }
      ParameterDescription
      endpointAPI call address, which can be copied from Queue Service > API Request Address in the TDMQ for CMQ console. img
      secretId, secretKeyTencentCloud API key, which can be copied on the Access Key > API Key Management page in the CAM console. img
      $queue_nameQueue name, which can be obtained on the Queue Service page in the TDMQ for CMQ console.

    Topic Model

    Directions

    1. Prepare the required resources and create a topic subscription and a subscriber.
      1. Create a topic subscription in the console or via TencentCloud API. To use TencentCloud API, you need to install the SDK 3.0 for PHP.
        $cred = new Credential($secretId, $secretKey);
        $httpProfile = new HttpProfile();
        $httpProfile->setEndpoint($endPoint);

        $clientProfile = new ClientProfile();
        $clientProfile->setHttpProfile($httpProfile);
        $client = new TdmqClient($cred, "ap-guangzhou", $clientProfile);

        $req = new CreateCmqTopicRequest();

        $params = array(
        "TopicName" => "topic_api1", // Topic name, which must be unique in the same region under the same account
        "FilterType" => 2, // Used to specify the message match policy for the topic. 1: Tag matching policy. 2: Routing matching policy
        "MsgRetentionSeconds" => 86400 // Message retention period. Value range: 60–86400 seconds (i.e., 1 minute–1 day)
        );
        $req->fromJsonString(json_encode($params));

        // Create a topic
        $resp = $client->CreateCmqTopic($req);
        ParameterDescription
        $endPointAPI call address, which can be copied from Queue Service > API Request Address in the TDMQ for CMQ console. img
        $secretId, $secretKeyTencentCloud API key, which can be copied on the Access Key > API Key Management page in the CAM console. img
      2. Create a subscriber in the console or via TencentCloud API. To use TencentCloud API, you need to install the SDK 3.0 for PHP.
        $cred = new Credential($secretId, $secretKey);
        $httpProfile = new HttpProfile();
        $httpProfile->setEndpoint($endPoint);

        $clientProfile = new ClientProfile();
        $clientProfile->setHttpProfile($httpProfile);
        $client = new TdmqClient($cred, "ap-guangzhou", $clientProfile);

        $req = new CreateCmqSubscribeRequest();

        $params = array(
        // Name of the topic for which to create a subscription
        "TopicName" => "topic_api1",
        // Subscription name
        "SubscriptionName" => "sub1",
        // Subscription protocol. Currently, two protocols are supported: HTTP and queue. To use the HTTP protocol, you need to build your own web server to receive messages. With the queue protocol, messages are automatically pushed to a CMQ queue and you can pull them concurrently.
        "Protocol" => "queue",
        // Endpoint that receives notifications, which varies by protocol: for HTTP, the endpoint must start with `http://`, and the `host` can be a domain or IP; for queue, `QueueName` should be entered.
        "Endpoint" => "topic_queue_api",
        // CMQ push server retry policy. Valid values: 1) BACKOFF_RETRY; 2) EXPONENTIAL_DECAY_RETRY.
        "NotifyStrategy" => "BACKOFF_RETRY",
        // The number of `BindingKey` cannot exceed 5, and the length of each `BindingKey` cannot exceed 64 bytes. This field indicates the filtering policy for subscribing to and receiving messages.
        "BindingKey" => array("a.b"),
        // Message filter tag (used for message filtering). The number of tags cannot exceed 5.
        // "FilterTag" => array("TAG"),
        // Push content format. Valid values: 1. JSON; 2. SIMPLIFIED, i.e., the raw format. If `Protocol` is `queue`, this value must be `SIMPLIFIED`. If `Protocol` is `http`, both options are acceptable, and the default value is `JSON`.
        "NotifyContentFormat" => "SIMPLIFIED"
        );
        $req->fromJsonString(json_encode($params));

        // Create a subscription
        $resp = $client->CreateCmqSubscribe($req);
        Note

        BindingKey and FilterTag should be set according to the type of subscribed topic; otherwise, they will be invalid.

        ParameterDescription
        $endPointAPI call address, which can be copied from Queue Service > API Request Address in the TDMQ for CMQ console. img
        $secretId, $secretKeyTencentCloud API key, which can be copied on the Access Key > API Key Management page in the CAM console. img
    2. Import CMQ files into the project.
    3. Create my_topic to publish messages.
      // Topic subscription name
      $topic_name = "php_topic_tag";
      // Authentication information
      $my_account = new Account($this->endpoint, $this->secretId, $this->secretKey);
      $my_topic = $my_account->get_topic($topic_name);
      ParameterDescription
      endpointAPI call address, which can be copied from Queue Service > API Request Address in the TDMQ for CMQ console. img
      secretId, secretKeyTencentCloud API key, which can be copied on the Access Key > API Key Management page in the CAM console. img
      $topic_nameTopic subscription name, which can be obtained on the Topic Subscription page in the TDMQ for CMQ console.
    4. Send tag messages.
      // Send tag messages
      $msg = "this is a test message for tag.";
      $msgid = $my_topic->publish_message($msg, array("TAG","TAG1"));
    5. Send route messages.
      // Send route messages.
      $msg = "this is a test message for route.";
      $msgid = $my_topic->publish_message($msg, array(), "a.b.c");
    6. The consumer can consume messages in the message queue subscribed to by the subscriber.
    Note:

    Above is a brief introduction to message production and consumption in two models. For more information, see Demo or TDMQ for CMQ code repository.

    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support