All Products
Search
Document Center

ApsaraVideo Media Processing:Initialize a client

Last Updated:Nov 15, 2024

This topic describes how to initialize the client of ApsaraVideo Media Processing (MPS) SDK for PHP V2.0.

Obtain an AccessKey pair from environment variables

You can define the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET to configure the default credentials. When you call an API operation, the system reads the AccessKey pair from the default credentials and uses the AccessKey pair to complete authentication. For more information, see Configure environment variables in Linux, macOS, and Windows.

Initialize the SDK client

  1. Initialize the configuration object Darabonba\OpenApi\Models\Config.

    use Darabonba\OpenApi\Models\Config;
    $config = new Config([
        // Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured. 
        "accessKeyId" => $accessKeyId,
        // Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
        "accessKeySecret" => $accessKeySecret
    ]);
    // Specify the endpoint that you want to access.
    $config->endpoint = "ecs-cn-hangzhou.aliyuncs.com";
    
  2. Instantiate a client and generate a client object from the AlibabaCloud\SDK\Mts\V20140618\Mts class.

    use AlibabaCloud\SDK\Mts\V20140618\Mts;
    
    $client = new Mts($config);

Sample code

In this example, the SearchPipeline operation is called.

<?php

namespace AlibabaCloud\SDK\Sample;

use AlibabaCloud\SDK\Mts\V20140618\Mts;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Mts\V20140618\Models\SearchPipelineRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

class Sample {

    /**
     * Use your AccessKey ID and AccessKey secret to initialize the client.
     * @return Mts Client
     */
    public static function createClient(){

        $config = new Config([
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured. 
                "accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured. 
                "accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
        ]);
        $config->endpoint = "mts.cn-hangzhou.aliyuncs.com";
        return new Mts($config);
    }

    /**
     * @param string[] $args
     * @return void
     */
    public static function main($args){
        $client = self::createClient();
        $searchPipelineRequest = new SearchPipelineRequest([
                // The state of the MPS queues that you want to query.
                "state" => "Paused",
                // The number of entries to return on each page.
                "pageSize" => 10,
                // The number of the current page.
                "pageNumber" => 1
        ]);
        $runtime = new RuntimeOptions([]);
        try {
            // Write your own code to display the response of the API operation if necessary.
            $client->searchPipelineWithOptions($searchPipelineRequest, $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. 
            // The error message.
            var_dump($error->message);
            // The URL of the corresponding error diagnostics page.
            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));