

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# AWS SDK for PHP バージョン 3 を使用した Amazon SQS でのロングポーリングの有効化
<a name="sqs-examples-enable-long-polling"></a>

ロングポーリングは、レスポンスの送信前にメッセージがキューで使用可能になるまで Amazon SQS が指定された時間待機できるようにすることで、空のレスポンス数を削減します。また、ロングポーリングでは、サーバーをサンプリングするのではなくすべてのサーバーをクエリすることによって、偽の空のレスポンスが排除されます。ロングポーリングを有効にするには、受信したメッセージについてゼロ以外の待機時間を指定します。詳細については、「[SQS ロングポーリング](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html)」を参照してください。

以下の例では、次の方法を示しています。
+ [SetQueueAttributes](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#setqueueattributes) を使用して、Amazon SQS キューの属性を設定し、ロングポーリングを有効にします。
+ [ReceiveMessage](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#receivemessage) を使用して、ロングポーリングで 1 つ以上のメッセージを取得する
+ [CreateQueue](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#createqueue) を使用して、ロングポーリングキューを作成する

AWS SDK for PHP 用のすべてのサンプルコードは [GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code) で入手できます。

## 認証情報
<a name="examplecredentials"></a>

サンプルコードを実行する前に、AWS の認証情報を設定します ([AWS SDK for PHP バージョン 3 AWS を使用した での認証](credentials.md) を参照)。AWS SDK for PHP からのインポート ([AWS SDK for PHP バージョン 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());
}
```