All Products
Search
Document Center

Artificial Intelligence Recommendation:SDK for PHP

Last Updated:Sep 06, 2023

SDK for PHP

Source code

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

Add dependencies

If you have installed Composer globally 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 Artificial Intelligence Recommendation (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 PHP, you must apply for an Alibaba Cloud AccessKey pair. Go to the AccessKey Pair page. Select an AccessKey pair for AIRec SDK for PHP. If no AccessKey pair is available, create an AccessKey pair and make sure that it is in the enabled state. For more information about how to create an AccessKey pair, see Create an AccessKey pair.

Step 3: Install the PHP development environment

AIRec SDK for PHP supports PHP 5.5.0 or later. You can install AIRec SDK for PHP on an on-premises server and set up the 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.

Push data

Note: For more information about the JSON format of the pushed data, see Push data.

<?php
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

// Download URL: https://github.com/aliyun/openapi-sdk-php
// User guide: https://github.com/aliyun/openapi-sdk-php/blob/master/README.md
// Set the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. 

AlibabaCloud::accessKeyClient(getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
                        ->regionId('cn-hangzhou')
                        ->asDefaultClient();

try {
    $result = AlibabaCloud::roa()
                          ->product('Airec')
                          // ->scheme('https') // https | http
                          ->version('2018-10-12')
                          ->pathPattern('/openapi/instances/[InstanceId]/tables/The name of the table that is used to receive data/actions/bulk')
                          ->method('POST')
                          ->options([
                                        'query' => [

                                        ],
                                    ])
                          ->body('JSON data')
                          ->request();
    print_r($result->toArray());
} catch (ClientException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
}

Obtain recommendation results

Note: For more information about the response parameters for obtaining recommendation results and common errors, see Obtain recommendation results.

<?php
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

// Download URL: https://github.com/aliyun/openapi-sdk-php
// User guide: https://github.com/aliyun/openapi-sdk-php/blob/master/README.md
// Set the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. 

AlibabaCloud::accessKeyClient(getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
                        ->regionId('cn-hangzhou')
                        ->asDefaultClient();

try {
    $result = AlibabaCloud::roa()
                          ->product('Airec')
                          // ->scheme('https') // https | http
                          ->version('2018-10-12')
                          ->pathPattern('/openapi/instances/airec-xxx/actions/recommend')
                          ->method('GET')
                          ->options([
                                        'query' => [
                                          'ReturnCount' => '10',
                                          'UserId' => '1234',
                                          'SceneId' => 'test01',
                                        ],
                                    ])

                          ->request();
    print_r($result->toArray());
} catch (ClientException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
}