This topic describes the prerequisites and sample code for using Simple Message Queue (SMQ) SDK for PHP to manage messages of a queue.
Prerequisites
SMQ SDK for PHP is installed. For more information, see Install SDK for PHP.
An endpoint and an access credential are configured. For more information, see Configure endpoints and access credentials.
The settings at the beginning of the CreateQueueAndSendMessage.php file are modified based on the comments.
// Replace the value with the path of the autoload.php file in the vendor directory downloaded by the composer. require_once __DIR__ . '/vendor/autoload.php'; // Declare the required PHP classes. use AliyunMNS\Client; use AliyunMNS\Requests\SendMessageRequest; use AliyunMNS\Requests\CreateQueueRequest; use AliyunMNS\Exception\MnsException;
Select an encoding method for the message body
If the message body does not contain special characters, we recommend that you do not use Base64 encoding.
To send a message, use the
message.setMessageBodyAsRawStringmethod to set the message body.To receive a message, use the
message.getMessageBodyAsRawStringmethod to obtain the message body.
Send a message
For more information about the complete sample code, see CreateQueueAndSendMessage.php.
// 1. Obtain a queue.
// By default, SMQ SDK for PHP performs Base64 encoding on sent messages and Base64 decoding on received messages.
// If you do not want the SDK to perform the preceding operations, you can set the $base64 parameter to FALSE when you call the getQueueRef() method: $queue = $this->client->getQueueRef($queueName, FALSE).
$queue = $this->client->getQueueRef($queueName);
$messageBody = "test";
// 2. Generate a SendMessageRequestItem object.
// You can configure the DelaySeconds and Priority properties of the SendMessageRequestItem object.
// For more information about the properties of a message object, see QueueMessage.
$bodyMD5 = md5(base64_encode($messageBody));
$request = new SendMessageRequestItem($messageBody);
try
{
$res = $queue->sendMessage($request)
// 3. The message is sent.
echo "MessageSent! \n";
}
catch (MnsException $e)
{
// 4. The message may fail to be sent if a network error occurs or the message is oversized. You can catch the exception to identify the cause and resolve the error.
echo "SendMessage Failed: " . $e;
return;
}Receive and delete a message
For more information about the complete sample code, see CreateQueueAndSendMessage.php.
$receiptHandle = NULL;
try
{
// 1. Call the receiveMessage() method.
// We recommend that you set the WaitSeconds parameter to 30 when you call the receiveMessage() method.
// If you set the waitSeconds parameter to a non-zero value, the request to extract messages is an HTTP long polling request. A response is returned only if the message is sent to the queue or the request times out. The maximum value of the WaitSeconds parameter is 30.
$res = $queue->receiveMessage(30);
echo "ReceiveMessage Succeed! \n";
if (strtoupper($bodyMD5) == $res->getMessageBodyMD5())
{
echo "You got the message sent by yourself! \n";
}
// 2. Obtain a receipt handle, which has a validity period and can be used to modify or delete the message. For more information, see QueueMessage.
$receiptHandle = $res->getReceiptHandle();
}
catch (MnsException $e)
{
// 3. The message may fail to be received if an error occurs. You can catch the exception to identify the cause and resolve the error.
echo "ReceiveMessage Failed: " . $e;
return;
}
// Specify custom logic to process the message based on your business requirements. This step is skipped in the sample code.
// If the program fails or freezes, the message can be consumed by other processes after a period that is specified by the VisibilityTimeout parameter elapses. This prevents the message from being lost.
// 4. After the message is consumed, you can delete the message from the queue.
try
{
// 5. Call the deleteMessage() method.
$res = $queue->deleteMessage($receiptHandle);
echo "DeleteMessage Succeed! \n";
}
catch (MnsException $e)
{
// 6. If an error occurs, you can catch the exception to identify the cause and resolve the error.
// If the receipt handle expires, the MessageNotExist error is returned. This error indicates that you cannot use the receipt handle to find the corresponding message.
// To ensure that you can consume the message before the receipt handle expires, you must set the VisibilityTimeout parameter to an appropriate value. You can call the changeMessageVisibility() method to modify the value of the VisibilityTimeout parameter.
echo "DeleteMessage Failed: " . $e;
return;
}