

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# AWS SDK for PHP 버전 3을 사용하는 Amazon SQS에서 긴 폴링 활성화
<a name="sqs-examples-enable-long-polling"></a>

긴 폴링은 응답을 전송하기 전에 대기열에서 메시지를 사용할 수 있을 때까지 Amazon SQS를 지정된 시간 동안 대기시켜 빈 응답의 개수를 줄입니다. 또한, 긴 폴링은 서버의 샘플링 대신에 모든 서버를 쿼리하여 False인 빈 응답을 제거합니다. 긴 폴링을 활성화하려면 수신된 메시지에 0이 아닌 대기 시간을 지정합니다. 자세한 내용은 [SQS 긴 폴링](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html)을 참조하세요.

다음 예제에서는 다음과 같은 작업을 하는 방법을 보여줍니다.
+ [SetQueueAttribute](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)를 사용하여 긴 폴링이 있는 하나 이상의 메시지를 검색합니다.
+ [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 SDK for PHP 버전 3을 AWS 사용하여 로 인증](credentials.md)에 설명된 대로 AWS 보안 인증을 구성합니다. 그 다음 [AWS SDK for PHP 버전 3 설치](getting-started_installation.md)에 설명된 AWS SDK for PHP를 가져옵니다.

## 긴 폴링을 활성화하기 위한 대기열의 속성 설정
<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());
}
```