2018-09-14 17:57:46 -07:00
< ? php
2020-04-23 16:08:15 -07:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2024-02-05 10:49:20 -07:00
2019-01-18 00:02:57 -08:00
// snippet-start:[cloudfront.php.getdistribution.complete]
// snippet-start:[cloudfront.php.getdistribution.import]
2018-09-14 17:57:46 -07:00
require 'vendor/autoload.php' ;
2024-02-05 10:49:20 -07:00
2018-09-14 17:57:46 -07:00
use Aws\Exception\AwsException ;
2024-02-05 10:49:20 -07:00
2019-01-18 00:02:57 -08:00
// snippet-end:[cloudfront.php.getdistribution.import]
2024-02-05 10:49:20 -07:00
2020-04-27 16:17:12 -07:00
/* ////////////////////////////////////////////////////////////////////////////
* Purpose: Gets information about an Amazon CloudFront distribution.
*
* Prerequisites: An existing Amazon CloudFront distribution.
*
* Inputs:
* - $cloudFrontClient: An initialized AWS SDK for PHP SDK client
* for CloudFront.
* - $distributionId: The distribution's ID.
*
* Returns: Information about the distribution; otherwise,
* the error message.
* ///////////////////////////////////////////////////////////////////////// */
2024-02-05 10:49:20 -07:00
2020-04-23 16:08:15 -07:00
// snippet-start:[cloudfront.php.getdistribution.main]
function getDistribution ( $cloudFrontClient , $distributionId )
{
try {
$result = $cloudFrontClient -> getDistribution ([
'Id' => $distributionId
]);
2024-02-05 10:49:20 -07:00
2020-04-27 16:17:12 -07:00
$message = '' ;
2024-02-05 10:49:20 -07:00
2020-04-23 16:08:15 -07:00
if ( isset ( $result [ 'Distribution' ][ 'Status' ])) {
2020-04-27 16:17:12 -07:00
$message = 'The status of the distribution with the ID of ' .
$result [ 'Distribution' ][ 'Id' ] . ' is currently ' .
$result [ 'Distribution' ][ 'Status' ];
}
2024-02-05 10:49:20 -07:00
2020-04-27 16:17:12 -07:00
if ( isset ( $result [ '@metadata' ][ 'effectiveUri' ])) {
$message .= ', and the effective URI is ' .
$result [ '@metadata' ][ 'effectiveUri' ] . '.' ;
2020-04-23 16:08:15 -07:00
} else {
2020-04-27 16:17:12 -07:00
$message = 'Error: Could not get the specified distribution. ' .
2020-04-23 16:08:15 -07:00
'The distribution\'s status is not available.' ;
}
2024-02-05 10:49:20 -07:00
2020-04-27 16:17:12 -07:00
return $message ;
2020-04-23 16:08:15 -07:00
} catch ( AwsException $e ) {
return 'Error: ' . $e -> getAwsErrorMessage ();
2024-02-05 10:49:20 -07:00
}
2020-04-23 16:08:15 -07:00
}
2024-02-05 10:49:20 -07:00
2020-04-23 16:08:15 -07:00
function getsADistribution ()
{
$distributionId = 'E1BTGP2EXAMPLE' ;
2024-02-05 10:49:20 -07:00
2020-04-23 16:08:15 -07:00
$cloudFrontClient = new Aws\CloudFront\CloudFrontClient ([
'profile' => 'default' ,
'version' => '2018-06-18' ,
'region' => 'us-east-1'
2018-09-21 18:59:28 -07:00
]);
2024-02-05 10:49:20 -07:00
2020-04-23 16:08:15 -07:00
echo getDistribution ( $cloudFrontClient , $distributionId );
2018-10-30 20:53:20 -07:00
}
2024-02-05 10:49:20 -07:00
2020-04-23 16:08:15 -07:00
// Uncomment the following line to run this code in an AWS account.
// getsADistribution();
2019-01-18 00:02:57 -08:00
// snippet-end:[cloudfront.php.getdistribution.main]
// snippet-end:[cloudfront.php.getdistribution.complete]
2020-04-27 16:17:12 -07:00
// snippet-sourceauthor:[pccornel (AWS)]