2019-01-30 22:12:21 -08:00
< ? php
2020-05-05 11:31:17 -07:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2024-01-16 10:41:11 -05:00
2019-01-30 22:12:21 -08:00
// snippet-start:[cloudsearch.php.sign_cloudsearch_domain_request.complete]
// snippet-start:[cloudsearch.php.sign_cloudsearch_domain_request.import]
2020-05-05 11:31:17 -07:00
require './vendor/autoload.php' ;
2019-01-30 22:12:21 -08:00
use Aws\Credentials\CredentialProvider ;
use Aws\Signature\SignatureV4 ;
use GuzzleHttp\Client ;
use GuzzleHttp\Psr7\Request ;
2024-02-05 10:49:20 -07:00
2019-01-30 22:12:21 -08:00
// snippet-end:[cloudsearch.php.sign_cloudsearch_domain_request.import]
2020-05-05 11:31:17 -07:00
/* ////////////////////////////////////////////////////////////////////////////
* Purpose: Searches an Amazon CloudSearch domain.
*
* Prerequisites: An existing Amazon CloudSearch domain.
2019-01-30 22:12:21 -08:00
*
2020-05-05 11:31:17 -07:00
* Inputs:
* - $client: An initialized Guzzle HTTP API client.
* - $domainName: The name of the domain to search.
* - $domainId: The endpoint ID of the domain.
* - $domainRegion: The AWS Region code for the domain.
* - $searchString: The query string to use for the search.
*
* Returns: Information about the search results; otherwise, the error.
* ///////////////////////////////////////////////////////////////////////// */
2019-01-30 22:12:21 -08:00
// snippet-start:[cloudsearch.php.sign_cloudsearch_domain_request.main]
2020-05-05 11:31:17 -07:00
function searchDomain (
$client ,
$domainName ,
$domainId ,
$domainRegion ,
$searchString
) {
$domainPrefix = 'search-' ;
$cloudSearchDomain = 'cloudsearch.amazonaws.com' ;
$cloudSearchVersion = '2013-01-01' ;
$searchPrefix = 'search?' ;
2019-01-30 22:12:21 -08:00
2020-05-05 11:31:17 -07:00
// Specify the search to send.
$request = new Request (
'GET' ,
" https:// $domainPrefix $domainName - $domainId . $domainRegion . " .
" $cloudSearchDomain / $cloudSearchVersion / " .
" $searchPrefix $searchString "
);
2019-01-30 22:12:21 -08:00
2020-05-05 11:31:17 -07:00
// Get default AWS account access credentials.
$credentials = call_user_func ( CredentialProvider :: defaultProvider ()) -> wait ();
2019-01-30 22:12:21 -08:00
2020-05-05 11:31:17 -07:00
// Sign the search request with the credentials.
$signer = new SignatureV4 ( 'cloudsearch' , $domainRegion );
$request = $signer -> signRequest ( $request , $credentials );
2019-01-30 22:12:21 -08:00
2020-05-05 11:31:17 -07:00
// Send the signed search request.
$response = $client -> send ( $request );
2019-01-30 22:12:21 -08:00
2020-05-05 11:31:17 -07:00
// 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 ;
2019-01-30 22:12:21 -08:00
}
2020-05-05 11:31:17 -07:00
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();
2019-01-30 22:12:21 -08:00
// snippet-end:[cloudsearch.php.sign_cloudsearch_domain_request.main]
// snippet-end:[cloudsearch.php.sign_cloudsearch_domain_request.complete]
// snippet-sourceauthor:[jschwarzwalder (AWS)]