Updated on 2024-08-06 GMT+08:00

PHP

Scenarios

To use PHP to call an API through app authentication, obtain the PHP SDK, create a new project, and then call the API by referring to the API calling example.

This section uses IntelliJ IDEA 2018.3.5 as an example.

Prerequisites

  • You have obtained API calling information. For details, see Preparation.
  • You have installed IntelliJ IDEA 2018.3.5 or a later version. If not, download it from the official IntelliJ IDEA website and install it.
  • You have installed PHP 8.0.3 or a later version. If not, download it from the official PHP 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
    
  • You have installed the PHP plug-in on IntelliJ IDEA. If not, install the PHP plug-in according to Figure 1.
    Figure 1 Installing the PHP plug-in

Obtaining the SDK

Log in to the APIG console, and download the SDK on the SDKs page by referring to section "SDKs" in the API Gateway User Guide.

Then obtain the ApiGateway-php-sdk.zip package. The following table shows the files decompressed from the package.

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.

    Figure 2 PHP

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

    Figure 3 Selecting the SDK directory of PHP after decompression

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

    Figure 4 Directory structure of the new project php

    Modify the parameters in sample code signer.php as required. For details about the sample code, see API Calling Example.

API Calling Example

  1. Import the PHP SDK to your code.

    1
    require 'signer.php';
    

  2. Generate a new signer and enter the AppKey and AppSecret.

    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 method, request URL, and body. (The body should contain the actual request content.)

    1
    2
    $req = new Request('GET', "https://c967a237-cd6c-470e-906f-a8655461897e.apigw.exampleRegion.com/app1?a=1");
    $req->body = '';
    

  4. Add the x-stage header to the request to specify an environment name. Add other headers to be signed as necessary.

    1
    2
    3
    $req->headers = array(
        'x-stage' => 'RELEASE',
    );
    

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