Signing custom Amazon CloudSearch domain requests with Amazon SDK for PHP Version 3 - Amazon SDK for PHP
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Signing custom Amazon CloudSearch domain requests with Amazon SDK for PHP Version 3

Amazon CloudSearch domain requests can be customized beyond what is supported by the Amazon SDK for PHP. In cases where you need to make custom requests to domains protected by IAM authentication, you can use the SDK’s credential providers and signers to sign any PSR-7 request.

For example, if you’re following Cloud Search’s Getting Started guide and want to use an IAM-protected domain for Step 3, you would need to sign and execute your request as follows.

The following examples show how to:

  • Sign a request with the Amazon signing protocol using SignatureV4.

All the example code for the Amazon SDK for PHP is available here on GitHub.


Before running the example code, configure your Amazon credentials, as described in Credentials. Then import the Amazon SDK for PHP, as described in Basic usage.

Sign Amazon CloudSearch domain request


require './vendor/autoload.php'; use Aws\Credentials\CredentialProvider; use Aws\Signature\SignatureV4; use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request;

Sample Code

function searchDomain( $client, $domainName, $domainId, $domainRegion, $searchString ) { $domainPrefix = 'search-'; $cloudSearchDomain = ''; $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();