

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在使用 适用于 PHP 的 Amazon SDK 版本 3 的 Amazon SQS 中启用长轮询
<a name="sqs-examples-enable-long-polling"></a>

在发送响应之前，长轮询使 Amazon SQS 等待指定的时间，以便消息在队列中变得可用，从而减少空响应的数量。此外，长轮询通过查询所有服务器而不是执行服务器采样，消除了假的空响应。要启用长轮询，请为接收的消息指定不为零的等待时间。要了解更多信息，请参阅 [SQS 长轮询](https://docs.amazonaws.cn/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html)。

以下示例演示如何：
+ 使用 [SetQueueAttributes](https://docs.amazonaws.cn/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#setqueueattributes) 设置 Amazon SQS 队列的属性，以启用长轮询。
+ 使用 [ReceiveMessage](https://docs.amazonaws.cn/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#receivemessage) 借助长轮询检索一条或多条消息。
+ 使用 [CreateQueue](https://docs.amazonaws.cn/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#createqueue) 创建长轮询队列。

适用于 PHP 的 Amazon SDKGitHub[ 上提供了](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code)的所有示例代码。

## 凭证
<a name="examplecredentials"></a>

运行示例代码之前，请配置您的 Amazon 凭证，如 [Amazon 使用 适用于 PHP 的 Amazon SDK 版本 3 进行身份验证](credentials.md) 中所述。然后导入 适用于 PHP 的 Amazon SDK，如 [安装 适用于 PHP 的 Amazon SDK 版本 3](getting-started_installation.md) 中所述。

## 设置队列属性以启用长轮询
<a name="set-attributes-on-a-queue-to-enable-long-polling"></a>

 **导入**。

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
use Aws\Sqs\SqsClient;
```

 **示例代码** 

```
$queueUrl = "QUEUE_URL";

$client = new SqsClient([
    'profile' => 'default',
    'region' => 'us-west-2',
    'version' => '2012-11-05'
]);

try {
    $result = $client->setQueueAttributes([
        'Attributes' => [
            'ReceiveMessageWaitTimeSeconds' => 20
        ],
        'QueueUrl' => $queueUrl, // REQUIRED
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}
```

## 使用长轮询检索消息
<a name="retrieve-messages-with-long-polling"></a>

 **导入**。

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
use Aws\Sqs\SqsClient;
```

 **示例代码** 

```
$queueUrl = "QUEUE_URL";

$client = new SqsClient([
    'profile' => 'default',
    'region' => 'us-west-2',
    'version' => '2012-11-05'
]);

try {
    $result = $client->receiveMessage([
        'AttributeNames' => ['SentTimestamp'],
        'MaxNumberOfMessages' => 1,
        'MessageAttributeNames' => ['All'],
        'QueueUrl' => $queueUrl, // REQUIRED
        'WaitTimeSeconds' => 20,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}
```

## 使用长轮询创建队列
<a name="create-a-queue-with-long-polling"></a>

 **导入**。

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
use Aws\Sqs\SqsClient;
```

 **示例代码** 

```
$queueName = "QUEUE_NAME";


$client = new SqsClient([
    'profile' => 'default',
    'region' => 'us-west-2',
    'version' => '2012-11-05'
]);

try {
    $result = $client->createQueue([
        'QueueName' => $queueName,
        'Attributes' => [
            'ReceiveMessageWaitTimeSeconds' => 20
        ],
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}
```