composer require tencentcloud/tencentcloud-sdk-php
require '/path/to/vendor/autoload.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 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. |
$secretId, $secretKey | TencentCloud API key, which can be copied on the Access Key > API Key Management page in the CAM console. |
// 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;}
Parameter | Description |
endpoint | API call address, which can be copied from Queue Service > API Request Address in the TDMQ for CMQ console. |
secretId, secretKey | TencentCloud API key, which can be copied on the Access Key > API Key Management page in the CAM console. |
$queue_name |
// 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;}
Parameter | Description |
endpoint | API call address, which can be copied from Queue Service > API Request Address in the TDMQ for CMQ console. |
secretId, secretKey | TencentCloud API key, which can be copied on the Access Key > API Key Management page in the CAM console. |
$queue_name |
$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);
Parameter | Description |
$endPoint | API call address, which can be copied from Queue Service > API Request Address in the TDMQ for CMQ console. |
$secretId, $secretKey | TencentCloud API key, which can be copied on the Access Key > API Key Management page in the CAM console. |
$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);
BindingKey
and FilterTag
should be set according to the type of subscribed topic; otherwise, they will be invalid.Parameter | Description |
$endPoint | API call address, which can be copied from Queue Service > API Request Address in the TDMQ for CMQ console. |
$secretId, $secretKey | TencentCloud API key, which can be copied on the Access Key > API Key Management page in the CAM console. |
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);
Parameter | Description |
endpoint | API call address, which can be copied from Queue Service > API Request Address in the TDMQ for CMQ console. |
secretId, secretKey | TencentCloud API key, which can be copied on the Access Key > API Key Management page in the CAM console. |
$topic_name | Topic subscription name, which can be obtained on the Topic Subscription page in the TDMQ for CMQ console. |
// Send tag messages$msg = "this is a test message for tag.";$msgid = $my_topic->publish_message($msg, array("TAG","TAG1"));
// Send route messages.$msg = "this is a test message for route.";$msgid = $my_topic->publish_message($msg, array(), "a.b.c");
Was this page helpful?