All Products
Search
Document Center

Artificial Intelligence Recommendation:SDK for PHP

Last Updated:Nov 11, 2024

This topic describes how to install and call the SDK for PHP of Artificial Intelligence Recommendation (AIRec) in detail. The main content includes how to call AIRec to create an instance, view the details of the instance by specifying the instance ID, and start the full data import task for the instance.

Prerequisites

To call Alibaba Cloud OpenAPI, you need to configure an AccessKey. Make sure that you have created an AccessKey. For more information, see Create an AccessKey pair. To avoid credential leaks, a common solution is to write them into environment variables. For more security solutions, see Best practices for using an access credential to call API operations.

Environment requirements

  • PHP version must be 5.6 or later.

  • Composer must be installed globally on your system. For more information, see installed globally.

Important

The version of PHP used to install the SDK by using Composer must be earlier than or equal to the version of PHP used to run the SDK. For example, the vendor folder generated after the SDK is installed in PHP verison 7.2 can be used only in PHP version 7.2 or later. If the vendor folder is copied to PHP version 5.6, the dependency is incompatible with PHP version 5.6. If you fail to install Composer due to network issues, you can run the following command to use the full image of Composer provided by Alibaba Cloud.

composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

Step 1: Import the SDK

Alibaba Cloud SDK supports both generalized and specialized methods to call OpenAPI. For more information, see Generic calls and specialized calls. Different methods require different SDKs.

Specialized calls

You can visit OpenAPI Explorer, find the service that you want to use, and view the supported languages and installation methods. Then, import the SDK of the service to your project. In this example, the SDK is imported as follows:

  1. Visit Artificial Intelligence Recommendation.

  2. In the Languages section, select the language of the SDK that you need.

  3. Select the Installation Method that you need, and copy the code to your project.

  4. Add the dependency to your project.

Run the following command to install SMS SDK for PHP:

composer require alibabacloud/airec-20201126 1.0.0

Generalized calls

The generalized call method does not depend on the SDK of any specific service. It only depends on the following core package: com.aliyun.tea-openapi. The configuration file for PHP is as follows. For the latest version, see tea-openapi.

composer require alibabacloud/darabonba-openapi

Step 2: Initialize the client

Make sure that you specify the correct endpoint based on the region of AIRec. For more information, see Regions and endpoints.

The following example uses specialized call code to describe the call process. For more information about the generalized call method, see Generic calls and specialized calls.

Use an AccessKey to initialize

Note

The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using the AccessKey pair to perform operations is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. We recommend that you do not save the AccessKey ID and AccessKey secret into your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account is compromised.

In this example, the AccessKey pair is saved in the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to implement identity authentication.

<?php

// This file is auto-generated, don't edit it. Thanks.
namespace AlibabaCloud\SDK\Sample;

use AlibabaCloud\SDK\Airec\V20201126\Airec;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

class Sample {

    /**
     * Initialize the Client with the AccessKey of the account
     * @return Airec Client
     */
    public static function createClient(){
        // The project code leakage may result in the leakage of AccessKey, posing a threat to the security of all resources under the account. The following code examples are for reference only.
        // It is recommended to use the more secure STS credential. For more credentials, please refer to: https://www.alibabacloud.com/help/en/alibaba-cloud-sdk-262060/latest/credentials-settings-1.
        $config = new Config([
            // Required, please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID is set.
            "accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
            // Required, please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_SECRET is set.
            "accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
        ]);
        // See https://api.alibabacloud.com/product/Airec.
        $config->endpoint = "airec.cn-hangzhou.aliyuncs.com";
        return new Airec($config);
    }
    }

Step 3: Call the AIRec API by using the initialized client

Note

After you initialize the client, you can call the AIRec API by using the client.

API name:CreateInstance

This API is used to create a subscription instance of AIRec. You do not need to specify parameters when you call this API. You need to configure the runtime settings based on your business requirements. You can also customize the runtime settings to meet specific requirements.

 // Runtime settings
 $runtime = new RuntimeOptions([]);

The following code provides a complete example of how to use an AccessKey to create a subscription instance of AIRec:

<?php

// This file is auto-generated, don't edit it. Thanks.
namespace AlibabaCloud\SDK\Sample;

use AlibabaCloud\SDK\Airec\V20201126\Airec;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

class Sample {

    /**
     * Initialize the Client with the AccessKey of the account
     * @return Airec Client
     */
    public static function createClient(){
        // The project code leakage may result in the leakage of AccessKey, posing a threat to the security of all resources under the account. The following code examples are for reference only.
        // It is recommended to use the more secure STS credential. For more credentials, please refer to: https://www.alibabacloud.com/help/en/alibaba-cloud-sdk-262060/latest/credentials-settings-1.
        $config = new Config([
            // Required, please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID is set.
            "accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
            // Required, please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_SECRET is set.
            "accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
        ]);
        // See https://api.alibabacloud.com/product/Airec.
        $config->endpoint = "airec.cn-hangzhou.aliyuncs.com";
        return new Airec($config);
    }

    /**
     * @param string[] $args
     * @return void
     */
    public static function main($args){
        $client = self::createClient();
        $runtime = new RuntimeOptions([]);
        $headers = [];
        try {
            // Copy the code to run, please print the return value of the API by yourself.
            $client->createInstanceWithOptions($headers, $runtime);
        }
        catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            // Only a printing example. Please be careful about exception handling and do not ignore exceptions directly in engineering projects.
            // print error message
            var_dump($error->message);
            // Please click on the link below for diagnosis.
            var_dump($error->data["Recommend"]);
            Utils::assertAsString($error->message);
        }
    }
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
    require_once $path;
}
Sample::main(array_slice($argv, 1));

API name:DescribeInstance

This API is used to view the details of an instance by specifying the instance ID. You need to configure the parameters and runtime settings based on your business requirements. You can also customize the runtime settings to meet specific requirements.

  // Runtime settings
  $runtime = new RuntimeOptions([]);
  $headers = [];
            // Set request parameters
            $client->describeInstanceWithOptions("airec-cn-****", $headers, $runtime);

The following code provides a complete example of how to use an AccessKey to view the details of an instance by specifying the instance ID:

<?php

// This file is auto-generated, don't edit it. Thanks.
namespace AlibabaCloud\SDK\Sample;

use AlibabaCloud\SDK\Airec\V20201126\Airec;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

class Sample {

    /**
     * Use your AccessKey ID and AccessKey secret to create a client.
     * @return Airec Client
     */
    public static function createClient(){
      
        $config = new Config([
            // Required. Make sure that the following environment variable is set in the code runtime environment: ALIBABA_CLOUD_ACCESS_KEY_ID.
            "accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
            // Required. Make sure that the following environment variable is set in the code runtime environment: ALIBABA_CLOUD_ACCESS_KEY_SECRET.
            "accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
        ]);
        // Endpoint: https://api.aliyun.com/product/Airec
        $config->endpoint = "airec.cn-hangzhou.aliyuncs.com";
        return new Airec($config);
    }

    /**
     * @param string[] $args
     * @return void
     */
    public static function main($args){
        $client = self::createClient();
        $runtime = new RuntimeOptions([]);
        $headers = [];
        try {
            // After the sample code is run, you can print the return value of the API call as required.
            $client->describeInstanceWithOptions("airec-cn-****", $headers, $runtime);
        }
        catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for reference only.
            // Display error messages
            var_dump($error->message);
            // Provide the URL that is used for troubleshooting
            var_dump($error->data["Recommend"]);
            Utils::assertAsString($error->message);
        }
    }
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
    require_once $path;
}
Sample::main(array_slice($argv, 1));

API name:RunInstance

This API is used to start an instance and begin the full data import task. You need to configure the parameters and runtime settings based on your business requirements. You can also customize the runtime settings to meet specific requirements.

   // Runtime settings
   $runtime = new RuntimeOptions([]);
   $headers = [];
            // Set request parameters
            $client->runInstanceWithOptions("airec-cn-****", $headers, $runtime);

The following code provides a complete example of how to use an AccessKeyto start an instance and begin the full data import task by specifying the instance ID:

<?php

// This file is auto-generated, don't edit it. Thanks.
namespace AlibabaCloud\SDK\Sample;

use AlibabaCloud\SDK\Airec\V20201126\Airec;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

class Sample {

    /**
     * Initialize the Client with the AccessKey of the account
     * @return Airec Client
     */
    public static function createClient(){
        // The project code leakage may result in the leakage of AccessKey, posing a threat to the security of all resources under the account. The following code examples are for reference only.
        // It is recommended to use the more secure STS credential. For more credentials, please refer to: https://www.alibabacloud.com/help/en/alibaba-cloud-sdk-262060/latest/credentials-settings-1.
        $config = new Config([
            // Required, please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID is set.
            "accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
            // Required, please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_SECRET is set.
            "accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
        ]);
        // See https://api.alibabacloud.com/product/Airec.
        $config->endpoint = "airec.cn-hangzhou.aliyuncs.com";
        return new Airec($config);
    }

    /**
     * @param string[] $args
     * @return void
     */
    public static function main($args){
        $client = self::createClient();
        $runtime = new RuntimeOptions([]);
        $headers = [];
        try {
            // Copy the code to run, please print the return value of the API by yourself.
            $client->runInstanceWithOptions("", $headers, $runtime);
        }
        catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            // Only a printing example. Please be careful about exception handling and do not ignore exceptions directly in engineering projects.
            // print error message
            var_dump($error->message);
            // Please click on the link below for diagnosis.
            var_dump($error->data["Recommend"]);
            Utils::assertAsString($error->message);
        }
    }
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
    require_once $path;
}
Sample::main(array_slice($argv, 1));