ListDistributions.php - AWS Code Sample

ListDistributions.php

ListDistributions.php demonstrates how to list Amazon CloudFront distributions.

<?php /* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ require 'vendor/autoload.php'; use Aws\CloudFront\CloudFrontClient; use Aws\Exception\AwsException; /* //////////////////////////////////////////////////////////////////////////// * Purpose: Gets information about Amazon CloudFront distributions. * * Prerequisites: At least one existing Amazon CloudFront distribution. * * Inputs: * - $cloudFrontClient: An initialized AWS SDK for PHP SDK client * for CloudFront. * * Returns: Information about existing distributions; otherwise, * the error message. * ///////////////////////////////////////////////////////////////////////// */ function listDistributions($cloudFrontClient) { try { $result = $cloudFrontClient->listDistributions([]); return $result; } catch (AwsException $e) { exit('Error: ' . $e->getAwsErrorMessage()); } } function listTheDistributions() { $cloudFrontClient = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-2' ]); $distributions = listDistributions($cloudFrontClient); if (count($distributions) == 0) { echo 'Could not find any distributions.'; } else { foreach ($distributions['DistributionList']['Items'] as $distribution) { echo 'The distribution with the ID of ' . $distribution['Id'] . ' has the status of ' . $distribution['Status'] . '.' . "\n"; } } } // Uncomment the following line to run this code in an AWS account. // listTheDistributions();

Sample Details

Service: cloudfront

Last tested: 2020-04-24

Author: pccornel (AWS)

Type: full-example