All Products
Search
Document Center

Alibaba Cloud SDK:Configure an HTTPS request

Last Updated:Jul 29, 2024

This topic describes how to configure an HTTPS request in Alibaba Cloud SDK V1.0 for PHP.

Configure an HTTPS request

You can configure settings to disable certificate verification. You can also configure other HTTP settings in Guzzle. For more information, see Request Options.

Important

By default, if you send an API request over HTTPS, the SDK enables certificate verification to verify the validity of SSL/TLS certificates. If no SSL/TLS certificate is configured in the development environment, an error that indicates the certificate verification fails is reported.

To ensure network communication security, we recommend that you enable certificate verification. If certificate verification must be disabled in the test environment, you can set the verify parameter to false.

Specialized calls

You can configure a request object to send an API request over HTTPS. Sample code:

<?php

require_once 'vendor/autoload.php';

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Ecs\Ecs;

try {
    // Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. 
    AlibabaCloud::accessKeyClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'));
    $request = Ecs::v20140526()->describeRegions();
    $result = $request
        ->scheme('https') // Specify the protocol over which API requests are sent. 
        ->verify(false) // Specify whether to enable certificate verification. A value of false indicates that the certificate verification is disabled. To ensure network communication security, we recommend that you enable certificate verification. 
        ->version('2014-05-26')
        ->product('Ecs')
        ->action('DescribeRegions')
        ->regionId('cn-hangzhou')
        ->request();
    print_r($result->toArray());
} catch (ClientException $exception) {
    // Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only. 
    echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
    // Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only. 
    echo $exception->getMessage() . PHP_EOL;
    echo $exception->getErrorCode() . PHP_EOL;
    echo $exception->getRequestId() . PHP_EOL;
    echo $exception->getErrorMessage() . PHP_EOL;
}

Generic calls

You can configure an HTTPS request to send an API request in the remote procedure call (RPC) style. Sample code:

<?php

require_once 'vendor/autoload.php';

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

try {
    // Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. 
    AlibabaCloud::accessKeyClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'));
    $result = AlibabaCloud::rpc()
        ->product('Ecs')
        ->version('2014-05-26')
        ->action('DescribeRegions')
        ->regionId('cn-hangzhou')
        ->scheme('https') // Specify the protocol over which API requests are sent. 
        ->verify(false) // Specify whether to enable certificate verification. A value of false indicates that the certificate verification is disabled. To ensure network communication security, we recommend that you enable certificate verification. 
        ->request();
    print_r($result->toArray());
} catch (ClientException $exception) {
    // Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only. 
    echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
    // Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only. 
    echo $exception->getMessage() . PHP_EOL;
    echo $exception->getErrorCode() . PHP_EOL;
    echo $exception->getRequestId() . PHP_EOL;
    echo $exception->getErrorMessage() . PHP_EOL;
}

You can configure an HTTPS request to send an API request in the resource-oriented architecture (ROA) style. Sample code:

<?php

require_once 'vendor/autoload.php';

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

try {
    // Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. 
    AlibabaCloud::accessKeyClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'));
    $result = AlibabaCloud::roa()
        ->product('CS')
        ->version('2015-12-15')
        ->action('DescribeClustersV1')
        ->pathPattern('/api/v1/clusters')
        ->regionId('cn-hangzhou')
        ->options([
            'query' => [
            ],
        ])
        ->scheme('https') // Specify the protocol over which API requests are sent. 
        ->verify(false) // Specify whether to enable certificate verification. A value of false indicates that the certificate verification is disabled. To ensure network communication security, we recommend that you enable certificate verification. 
        ->request();
    print_r($result->toArray());
} catch (ClientException $exception) {
    echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
    echo $exception->getMessage() . PHP_EOL;
    echo $exception->getErrorCode() . PHP_EOL;
    echo $exception->getRequestId() . PHP_EOL;
    echo $exception->getErrorMessage() . PHP_EOL;
}

Specify a custom certificate

To ensure network communication security, we recommend that you send API requests over HTTPS and enable certificate verification. You can use one of the following methods to perform certificate verification in Alibaba Cloud SDK V1.0 for PHP.

  1. Specify a certificate for an API request:

<?php

use AlibabaCloud\Client\AlibabaCloud;

$request = AlibabaCloud::rpc()
                       ->product('Sts')
                       ->version('2015-04-01')
                       ->action('GenerateSessionAccessKey')
                       ->host('sts.ap-northeast-1.aliyuncs.com');

// Query a certificate in the operating system.
$request->verify(true);

// Use the specified certificate.
$request->verify(['verify' => '/path/to/cert.pem']);

// Use the specified certificate and password.
$request->verify(['verify' => ['/path/to/cert.pem','password']]);

2. Specify a certificate for the client:

<?php

use AlibabaCloud\Client\AlibabaCloud;

// Query a certificate in the operating system.
AlibabaCloud::getDefaultClient()
            ->verify(true)
            ->asDefaultClient();

// Use the specified certificate.
AlibabaCloud::getDefaultClient()
            ->verify(['verify' => '/path/to/cert.pem'])
            ->asDefaultClient();

// Use the specified certificate and password.
AlibabaCloud::getDefaultClient()
            ->verify(['/path/to/cert.pem','password'])
            ->asDefaultClient();