All Products
Search
Document Center

Alibaba Cloud SDK:Get started with Alibaba Cloud Darabonba SDK for PHP

最終更新日:Jun 19, 2024

This topic describes how to install the environment for using Alibaba Cloud Darabonba SDK for PHP and run the sample code to call an API operation.

Preparations

Prerequisites

  • PHP 5.6 or later is downloaded and installed. To download the required PHP version, go to the Downloads page on the PHP official website.

  • Composer is installed globally. For more information, see Globally.

  • An integrated development environment (IDE) is downloaded and installed. In this example, PhpStorm is used. To download PhpStorm, visit the PhpStorm official website.

  • An AccessKey pair is created. When you call an API operation, you must use an AccessKey pair to complete identity authentication. An AccessKey pair consists of an AccessKey ID and an AccessKey secret. For more information about how to create an AccessKey pair, see Create an AccessKey pair.

PHP version

Run the php -v command on the terminal to check the PHP version.

Run the sample code

Step 1: Download an SDK project

  1. Log on to OpenAPI Explorer.

  2. In the top navigation bar, click Select a cloud service. In the panel that appears, select the cloud service that you want to manage. The page of the selected cloud service appears.

    image

  3. In the upper-right corner, click Debug. Then, a page with three columns appears.

    Figure 1

    image

    Figure 2

    image.png

  4. Select the API operation that you want to call in the leftmost column. Configure the required parameters in the middle column. Then, click the SDK Sample Code tab in the rightmost column.

  5. Select V2.0 from the SDK Version drop-down list. Select a programming language based on your business requirements.

    Note

    For some cloud services, you cannot select the version of an SDK. You can view the SDK versions that are supported by the selected cloud service in Figure 1 of Step 3.

  6. Click Download Project to download the sample code.

  7. If you want to generate generic code, turn on Common Request. For more information, see Generic calls and specialized calls.

Step 2: Configure environment variables

Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

Important

After you configure environment variables, you must restart your development tool. Otherwise, the environment variables may fail to be read.

  • Linux and macOS

    1. Create the .bash_profile file.

      Note

      If you configure environment variables for the first time, you can run the touch ~/.bash_profile command to create the configuration file. If the configuration file already exists, run the vim ~/.bash_profile command to edit the configuration file.

      touch ~/.bash_profile
      vim ~/.bash_profile
    2. Configure the .bash_profile file.

      # Replace <ACCESS_KEY_ID> and <ACCESS_KEY_SECRET> in the following commands with your AccessKey ID and AccessKey secret. 
      # Replace the variable names based on your business requirements, such as OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
      export ALIBABA_CLOUD_ACCESS_KEY_ID=<ACCESS_KEY_ID>
      export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<ACCESS_KEY_SECRET>

      Save the file after the configuration.

    3. Use the .bash_profile file.

      # Run the following command in the configuration file.
      source ~/.bash_profile
      # Check whether the configuration takes effect.
      echo $ALIBABA_CLOUD_ACCESS_KEY_ID

      If the valid AccessKey ID is returned, the configuration is successful.

  • Windows

    Use GUI

    • Procedure

      If you want to use GUI to configure environment variables in Windows 10, perform the following steps:

      On the Windows desktop, right-click This PC and select Properties. On the page that appears, click Advanced system settings. In the System Properties dialog box, click Environment Variables on the Advanced tab. In the Environment Variables dialog box, click New in the User variables or System variables section. Then, configure the variables described in the following table.

      Variable

      Example

      AccessKey ID

      • Variable name: ALIBABA_CLOUD_ACCESS_KEY_ID

      • Variable value: LTAI4GDty8ab9W4Y1D****

      AccessKey Secret

      • Variable name: ALIBABA_CLOUD_ACCESS_KEY_SECRET

      • Variable value: IrVTNZNy5yQelTETg0cZML3TQn****

    • Check whether the configuration is successful.

      On the Windows desktop, click Start or press Win + R. In the Run dialog box, enter cmd. Then, click OK or press the Enter key. On the page that appears, run the echo %ALIBABA_CLOUD_ACCESS_KEY_ID% and echo %ALIBABA_CLOUD_ACCESS_KEY_SECRET% commands. If the valid AccessKey pair is returned, the configuration is successful.

    Use CMD

    • Procedure

      Open a Command Prompt window as an administrator and run the following commands to add environment variables in the operating system:

      setx ALIBABA_CLOUD_ACCESS_KEY_ID LTAI4GDty8ab9W4Y1D**** /M
      setx ALIBABA_CLOUD_ACCESS_KEY_SECRET IrVTNZNy5yQelTETg0cZML3TQn**** /M

      /M indicates that the environment variable is of system level. You cannot use this parameter when you configure a user-level environment variable.

    • Check whether the configuration is successful.

      On the Windows desktop, click Start or press Win + R. In the Run dialog box, enter cmd. Then, click OK or press the Enter key. On the page that appears, run the echo %ALIBABA_CLOUD_ACCESS_KEY_ID% and echo %ALIBABA_CLOUD_ACCESS_KEY_SECRET% commands. If the valid AccessKey pair is returned, the configuration is successful.

    Use Windows PowerShell

    In PowerShell, configure new environment variables. The environment variables apply to all new sessions.

    [System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_ID', 'LTAI4GDty8ab9W4Y1D****', [System.EnvironmentVariableTarget]::User)
    [System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_SECRET', 'IrVTNZNy5yQelTETg0cZML3TQn****', [System.EnvironmentVariableTarget]::User)

    Configure environment variables for all users. You must run the following commands as a administrator.

    [System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_ID', 'LTAI4GDty8ab9W4Y1D****', [System.EnvironmentVariableTarget]::Machine)
    [System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_SECRET', 'IrVTNZNy5yQelTETg0cZML3TQn****', [System.EnvironmentVariableTarget]::Machine)

    Configure temporary environment variables. The environment variables apply only to the current session.

    $env:ALIBABA_CLOUD_ACCESS_KEY_ID = "LTAI4GDty8ab9W4Y1D****"
    $env:ALIBABA_CLOUD_ACCESS_KEY_SECRET = "IrVTNZNy5yQelTETg0cZML3TQn****"

    In PowerShell, run the Get-ChildItem env:ALIBABA_CLOUD_ACCESS_KEY_ID and Get-ChildItem env:ALIBABA_CLOUD_ACCESS_KEY_SECRET commands. If the valid AccessKey pair is returned, the configuration is successful.

Step 3: Run the code

  1. Run the code in PhpStorm. After the code is run, view the returned information and request ID on the terminal.

  2. If you call an API operation to create an instance, the instance ID is also returned. You can log on to the console of the Alibaba Cloud service to verify the returned information based on the instance ID.