

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

# AWS SDK for PHP バージョン 3 によるカスタム Amazon CloudSearch ドメインリクエストへの署名
<a name="service_cloudsearch-custom-requests"></a>

Amazon CloudSearch ドメインリクエストは、AWS SDK for PHP でサポートされているもの以外でもカスタマイズできます。IAM 認証によって保護されているドメインへのカスタムリクエストを作成する必要がある場合は、SDK の認証情報プロバイダと署名者を使用して、任意の [PSR-7 リクエスト](https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Psr.Http.Message.RequestInterface.html)に署名できます。

たとえば、「[Cloud Search の使用開始](https://docs.aws.amazon.com/cloudsearch/latest/developerguide/getting-started.html)」に従って、[ステップ 3](https://docs.aws.amazon.com/cloudsearch/latest/developerguide/getting-started-search.html) で IAM で保護されているドメインを使用する場合は、次のようにリクエストに署名して実行する必要があります。

以下の例では、次の方法を示しています。
+ [SignatureV4](https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.Signature.SignatureV4.html#_signRequest) を使用して、AWS 署名プロトコルでリクエストに署名します。

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) を参照)。

## Amazon CloudSearch ドメインリクエストに署名
<a name="sign-cslong-domain-request"></a>

 **インポート**。

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

use Aws\Credentials\CredentialProvider;
use Aws\Signature\SignatureV4;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
```

 **サンプルコード** 

```
function searchDomain(
    $client,
    $domainName,
    $domainId,
    $domainRegion,
    $searchString
) {
    $domainPrefix = 'search-';
    $cloudSearchDomain = 'cloudsearch.amazonaws.com';
    $cloudSearchVersion = '2013-01-01';
    $searchPrefix = 'search?';

    // Specify the search to send.
    $request = new Request(
        'GET',
        "https://$domainPrefix$domainName-$domainId.$domainRegion." .
            "$cloudSearchDomain/$cloudSearchVersion/" .
            "$searchPrefix$searchString"
    );

    // Get default AWS account access credentials.
    $credentials = call_user_func(CredentialProvider::defaultProvider())->wait();

    // Sign the search request with the credentials.
    $signer = new SignatureV4('cloudsearch', $domainRegion);
    $request = $signer->signRequest($request, $credentials);

    // Send the signed search request.
    $response = $client->send($request);

    // Report the search results, if any.
    $results = json_decode($response->getBody());

    $message = '';

    if ($results->hits->found > 0) {
        $message .= 'Search results:' . "\n";

        foreach ($results->hits->hit as $hit) {
            $message .= $hit->fields->title . "\n";
        }
    } else {
        $message .= 'No search results.';
    }

    return $message;
}

function searchADomain()
{
    $domainName = 'my-search-domain';
    $domainId = '7kbitd6nyiglhdtmssxEXAMPLE';
    $domainRegion = 'us-east-1';
    $searchString = 'q=star+wars&return=title';
    $client = new Client();

    echo searchDomain(
        $client,
        $domainName,
        $domainId,
        $domainRegion,
        $searchString
    );
}

// Uncomment the following line to run this code in an AWS account.
// searchADomain();
```