tencent cloud

Feedback

SDK for PHP

Last updated: 2022-06-10 16:19:26

    Overview

    This document uses PHP as an example to describe how to connect to TDMQ for Pulsar to send and receive messages over the HTTP protocol.

    Prerequisites

    Directions

    1. Prepare the environment.
      1. Add dependencies.
        composer require tencentcloud/tencentcloud-sdk-php
      2. Add references.
        require '/path/to/vendor/autoload.php';
        Note

        For more information, see SDK for PHP 3.0 Installation and Use Guide.

    2. Create the TDMQ client.
    // Authentication information
    $cred = new Credential($secretId, $secretKey);
    $httpProfile = new HttpProfile();
    $httpProfile->setEndpoint($endpoint);
    $clientProfile = new ClientProfile();
    $clientProfile->setHttpProfile($httpProfile);
    // Create the TDMQ client
    $client = new TdmqClient($cred, $region, $clientProfile);
    Parameter Description
    $secretId、$secretKey TencentCloud API key, which can be copied on the Access Key > API Key Management page in the CAM console. img
    $endpoint API request domain: tdmq.tencentcloudapi.com.
    $region Cluster region.
    3. Send messages.
    $req = new SendMessagesRequest();

    $params = array(
    // Authorized role key
    "StringToken" => $token,
    // Topic name in the following format: cluster (tenant) ID/namespace/topic name
    "Topic" => $fullTopicName,
    // Message content
    "Payload" => "this is a new message.",
    // Message sending timeout period
    "SendTimeout" => 3000
    );
    $req->fromJsonString(json_encode($params));

    // Send the message
    $resp = $client->SendMessages($req);
    Parameter Description
    $token Role key, which can be copied in the Key column on the Role Management page. img
    $userName Role name, which can be copied in the Name column on the Role Management page.
    $fullTopicName Topic name in the following format: cluster (tenant) ID/namespace/topic name, such as `pulsar-xxx/sdk_http/topic1`. You can directly copy it on the Topic Management page in the console.
    4. Consume messages.
    $req = new ReceiveMessageRequest();

    $params = array(
    // Topic name in the following format: cluster (tenant) ID/namespace/topic name
    "Topic" => $fullTopicName,
    // Subscription name
    "SubscriptionName" => $subName,
    // Messages received by the consumer will first be stored in the `receiverQueueSize` queue to tune the message receiving rate
    "ReceiverQueueSize" => 10,
    // It is used to determine the position where the consumer initially receives messages. Valid values: Earliest, Latest.
    "SubInitialPosition" => "Latest"
    );
    $req->fromJsonString(json_encode($params));

    // Receive messages
    $resp = $client->ReceiveMessage($req);
    Parameter Description
    $fullTopicName Topic name in the following format: cluster (tenant) ID/namespace/topic name, such as `pulsar-xxx/sdk_http/topic1`. You can directly copy it on the Topic Management page in the console.
    $subName Subscription name, which can be copied on the Cluster > Consumer tab in the console.
    5. Acknowledge messages.
    $req = new AcknowledgeMessageRequest();

    $params = array(
    // ID of the message to be acknowledged
    "MessageId" => $messageId,
    // Topic name in the following format: cluster (tenant) ID/namespace/topic name
    "AckTopic" => $fullTopicName,
    // Subscription name
    "SubName" => $subName
    );
    $req->fromJsonString(json_encode($params));

    // Acknowledge messages
    $resp = $client->AcknowledgeMessage($req);
    Parameter Description
    $messageId Message ID obtained after message consumption.
    $fullTopicName Topic name in the following format: cluster (tenant) ID/namespace/topic name, such as `pulsar-xxx/sdk_http/topic1`. You can directly copy it on the Topic Management page in the console.
    $subName Subscription name, which can be copied on the Cluster > Consumer tab in the console.
    Note:

    Above is a brief introduction to message sending and receiving operations. For more information, see Demo or API Explorer.

    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