All Products
Search
Document Center

SDK for PHP

Last Updated: Mar 30, 2021

Use the SDK for PHP

URL of the source code

You can view the source code of Alibaba Cloud SDK for PHP at https://github.com/aliyun/openapi-sdk-php.

Install dependencies

If you have installed Composer in your system, run the following command in the project directory to install Alibaba Cloud SDK for PHP as a dependency:

composer require alibabacloud/sdk

If the installation fails due to network issues, you can use the full image of Alibaba Cloud Composer. For more information, see Alibaba Cloud Composer full image.

For more information about how to install Alibaba Cloud SDK for PHP by using Composer or other methods, see Installation in GitHub.

Procedure

To use AIRec SDK for PHP, perform the following steps:

Step 1: Create an Alibaba Cloud account

For more information, see Sign up with Alibaba Cloud. We recommend that you complete real-name verification at your earliest opportunity. Otherwise, you cannot use some Alibaba Cloud services.

Step 2: Obtain an Alibaba Cloud AccessKey pair

Before you use AIRec SDK for Java, you must apply for an Alibaba Cloud AccessKey pair.

Go to the Security Management page. Select an AccessKey pair for AIRec SDK for Java. The AccessKey pair consists of an AccessKey ID and an AccessKey secret. If no AccessKey pair is available, create one and make sure that it is in the Enable state. For more information about how to create an AccessKey pair, see Preparations.

Step 3: Set up the PHP development environment

AIRec SDK for PHP supports PHP 5.5.0 or later. You can install AIRec SDK for PHP on your on-premises machine and set up a PHP development environment.

Step 4: Use AIRec SDK for PHP

The following code uses the Recommend operation as an example to show how to use AIRec SDK for PHP.

<? php
include_once 'vendor/autoload.php';

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

use AlibabaCloud\Airec\Airec;

// 1. Create a client and specify the region ID.
AlibabaCloud::accessKeyClient($accessKeyID, $accessKeyIDSecret)
    ->regionId('cn-hangzhou')
    ->asDefaultClient();

try {
    // 2. Create a request.
    $request = Airec::v20181012()->recommend();

    // 3. Configure parameters for the request.
    $result = $request->debug(false)
        ->withSceneId(1)
        ->withUserId(1)
        ->withItems('1:item')
        ->withReturnCount(5)
        ->withInstanceId('airec-cn-xxx')
        ->request();   // Initiate the request and return results. The request must be at the end of the configuration.

    // 4. Return results.
    echo $result;

// 5. Catch exceptions.
} catch (ClientException $exception) {
    print_r($exception->getErrorMessage());
} catch (ServerException $exception) {
    print_r($exception->getErrorMessage());
}

Example for pushed data

<? php
include_once 'vendor/autoload.php';

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

use AlibabaCloud\Airec\Airec;

AlibabaCloud::accessKeyClient($accessKeyID, $accessKeyIDSecret)
    ->regionId('cn-hangzhou')
    ->asDefaultClient();

$content = "[{\"cmd\":\"ADD\",\"fields\":{\"id\":\"qww\",\"name\":\"xzdfsffds\"}},{\"cmd\":\"ADD\",\"fields\":{\"id\":\"13224\",\"name\":\"zdfsdg\"}}]";

try {
    $request = Airec::v20181012()->pushDocument();
    $result = $request->debug(false)
        ->withInstanceId('airec-cn-xxx')
        ->withTableName("item")
        ->body($content)
        ->request();

    echo $result;

} catch (ClientException $exception) {
    print_r($exception->getErrorMessage());
} catch (ServerException $exception) {
    print_r($exception->getErrorMessage());
}