All Products
Search
Document Center

ApsaraVideo Live:Use the server SDK for PHP

Last Updated:Dec 26, 2023

This topic describes how to use the server SDK for PHP provided by ApsaraVideo Live and provides relevant sample code. This topic uses the API operation that adds a domain name as an example to show you how to call an ApsaraVideo Live API operation.

Prerequisites

  • PHP 5.5.0 or later is installed.

  • The server SDK for PHP is downloaded. For more information, see SDK download.

Procedure

  1. Install ApsaraVideo Live SDK.

    composer require alibabacloud/live

    Note

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

  2. Create a configuration file named config.ini and place it in the conf directory. Include your AccessKey ID and AccessKey secret in the configuration file. Example:

    [default]
    access_key_id = YOUR_ACCESS_KEY_ID
    access_key_secret = YOUR_ACCESS_KEY_SECRET

    Replace YOUR_ACCESS_KEY_ID and YOUR_ACCESS_KEY_SECRET with your actual AccessKey ID and AccessKey secret.

    Then, you can use the following PHP code to read the configuration file and invoke the SDK.

  3. Initialize the client.

    <?php
    
    use AlibabaCloud\Client\AlibabaCloud;
    // Read the configuration file.
    $config = parse_ini_file('conf/config.ini');
    
    // The AccessKey pair of an Alibaba Cloud account has access permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. 
    // We recommend that you not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
    // In this example, the AccessKey pair is obtained from the configuration file to authenticate API accesses. 
    AlibabaCloud::accessKeyClient($config['access_key_id'], $config['access_key_secret'])->asDefaultClient();
  4. Call API operations by using the Remote Procedure Call (RPC) style. The following sample code uses the AddLiveDomain operation as an example.

    <?php
    
    use AlibabaCloud\Client\AlibabaCloud;
    use AlibabaCloud\Client\Exception\ClientException;
    use AlibabaCloud\Client\Exception\ServerException;
    
    try {
        $result = AlibabaCloud::rpc()
                              ->product('live')
                              ->version('2016-11-01')
                              ->action('AddLiveDomain')
                              ->method('POST')
                              ->request();
    
        print_r($result->toArray());
    
    } catch (ClientException $exception) {
        print_r($exception->getErrorMessage());
    } catch (ServerException $exception) {
        print_r($exception->getErrorMessage());
    }

    For more information, see List of operations by function.