Updated on 2023-01-17 GMT+08:00

PHP

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 from the IntelliJ IDEA official website and install it.
  • Download the PHP installation package 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 API Gateway console and choose Help Center > SDK Process Flow. Then download the SDK. For details, see section "SDKs" in the User Guide.

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

  1. Import the PHP SDK to your code.

    1
    require 'signer.php';
    

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

    1
    2
    3
    $signer = new Signer();
    $signer->Key = 'QTWAOY*********QVKYUC';
    $signer->Secret = "MFyfvK41ba2giqM7**********KGpownRZlmVmHc";
    

  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);