Updated on 2026-02-09 GMT+08:00

PHP Signing Guide

This section uses IntelliJ IDEA as an example to describe how to integrate the PHP SDK for API request signing. You can import the sample project in the code package, and integrate the signing SDK into your application by referring to the API calling example.

Preparing the Environment

  • Download IntelliJ IDEA 2018.3.5 or later from the IntelliJ IDEA official website and install it.
  • Download the PHP 8.0.3 or later from the PHP official website and install it.
  • Copy the php.ini-production file from the PHP installation directory to the C:\windows\ directory, rename the file as php.ini, and then add the following lines to the file:
    1
    2
    3
    extension_dir = "{PHP installation directory}/ext"
    extension=openssl
    extension=curl
    
  • Install the PHP plug-in on IDEA.

Obtaining the SDK

Log in to the APIG console and choose Help Center > SDK Process Flow. Then download the SDK.

Decompress the downloaded package to the current folder. The following table shows the directory structure.

Name

Description

signer.php

SDK code

index.php

Sample code

Creating a Project

  1. Start IDEA and choose File > New > Project.

    On the displayed New Project page, choose PHP and click Next.

  2. Click ..., select the directory where the SDK is decompressed, and click Finish.

  3. View the directory structure shown in the following figure.

Request Signing and API Calling

After the calling information is modified, the sample code can be directly called. For details about the calling information, see Preparations.

  1. Import the PHP SDK to your code.

    1
    require 'signer.php';
    

  2. Generate a new signer and enter the AK and SK.

    1. In this example, the AK and SK stored in the environment variables are used. Specify the environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment first. The following uses Linux as an example to describe how to set the obtained AK/SK as environment variables.
      1. Open the terminal and run the following command to open the environment variable configuration file:

        vi ~/.bashrc

      2. Set environment variables, save the file, and exit the editor.
        export CLOUD_SDK_AK="Obtained AK"
        export CLOUD_SDK_SK="Obtained SK"
      3. Run the following command to apply the modification:

        source ~/.bashrc

    2. Generate a new signer and enter the configured environment variables.
      1
      2
      3
      $signer = new Signer();
      $signer->Key = getenv('CLOUD_SDK_AK');
      $signer->Secret = getenv('CLOUD_SDK_SK');
      

  3. Generate a new request, and specify the domain name, method, request URI, and body.

    1
    2
    3
    4
    //The following example shows how to set the request URL and parameters to query a VPC list.
    $req = new Request('GET', 'https://service.region.example.com/v1/{project_id}/vpcs?limit=1');
    //Add a body if you have specified the PUT or POST method. Special characters, such as the double quotation mark ("), contained in the body must be escaped.
    $req->body = '';
    

  4. Add other headers required for request signing or other purposes. For example, add the X-Project-Id header in multi-project scenarios or the X-Domain-Id header for a global service.

    1
    2
    3
    4
    //Add header parameters, for example, X-Domain-Id for invoking a global service and X-Project-Id for invoking a project-level service.
    $req->headers = array(
       'X-Project-Id' => 'xxx',
    );
    

  5. Execute the following function to generate a $curl context variable.

    1
    $curl = $signer->Sign($req);
    

  6. Access the API and view the access result.

    1
    2
    3
    4
    $response = curl_exec($curl);
    echo curl_getinfo($curl, CURLINFO_HTTP_CODE);
    echo $response;
    curl_close($curl);