Updated on 2024-01-26 GMT+08:00

PHP

Scenarios

To use PHP to call an API through App authentication, obtain the PHP SDK, create a 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 Preparations.
  • You have installed the development tool and PHP development environment. For details, see Preparations.
    • 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:
      extension_dir = "PHP installation directory/ext"
      extension=openssl
      extension=curl
    • You have installed the PHP plug-in on IntelliJ IDEA. Otherwise, install it according to Figure 1.
      Figure 1 Installing the PHP plug-in

Obtaining the SDK

Old version: Log in to the ROMA Connect console, choose API Connect > API Calling > SDKs, and download the SDK.

New version: Log in to the ROMA Connect console, choose API Connect > Credentials > SDKs, and download the SDK.

The following shows the directory structure after the decompression.

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 New Project
  2. Click ..., select the directory where the SDK is decompressed, and click Finish.
    Figure 3 Selecting the SDK directory after decompression
  3. View the directory structure of the project.
    Figure 4 Directory structure of the new project

    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.
    require 'signer.php';
  2. Generate a signer and enter the key and secret of the authorized credential. For details about how to obtain the information, see Obtaining API Calling Information.
    $signer = new Signer();
    // Directly writing AK/SK in code is risky. For security, encrypt your AK/SK and store them in the configuration file or environment variables. 
    // In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, set environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK. 
    $signer->Key = getenv('HUAWEICLOUD_SDK_AK');
    $signer->Secret = getenv('HUAWEICLOUD_SDK_SK');
  3. Generate a request, and specify the method, request URL, and body. For details about how to obtain the information, see Obtaining API Calling Information.
    $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 the environment. Add other headers to sign as required.
    $req->headers = array(
        'x-stage' => 'RELEASE',
    );
  5. Execute the following function to generate a $curl context variable.
    $curl = $signer->Sign($req);
  6. If the subdomain name allocated by the system is used to access the API of HTTPS requests, ignore the certificate verification. Otherwise, skip this step.
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  7. Access the API and view the access result.
    $response = curl_exec($curl);
    echo curl_getinfo($curl, CURLINFO_HTTP_CODE);
    echo $response;
    curl_close($curl);