Updated on 2023-12-07 GMT+08:00

Application PHP SDK

IoTDA provides an application SDK in PHP for developers. This topic describes how to install and configure the PHP SDK and how to use it to call application-side APIs.

Currently, the SDK AK/SK authentication supports only the basic edition. You are advised to use token authentication for standard and enterprise editions.

Obtaining and Installing the SDK

  1. Install the PHP development environment.

    Visit the PHP website, and download and install the PHP development environment.

    The PHP SDK can be used in PHP 5.6 and later versions.

  2. Install Composer.

    curl -sS https://getcomposer.org/installer | php

  3. Install the PHP SDK.

    composer require huaweicloud/huaweicloud-sdk-php

  4. Introduce the autoload.php file of Composer.

    require 'path/to/vendor/autoload.php';

Code Sample

The following code sample shows how to use the PHP SDK to call API Querying the Device List.

  1. Create a credential.
  2. Create and initialize an IoTDAClient instance.
  3. Instantiate a request object.
  4. Call the API for querying the device list.

    <?php
    namespace HuaweiCloud\SDK\IoTDA\V5\Model;
    require_once "vendor/autoload.php";
    use HuaweiCloud\SDK\Core\Auth\BasicCredentials;
    use HuaweiCloud\SDK\Core\Http\HttpConfig;
    use HuaweiCloud\SDK\Core\Exceptions\ConnectionException;
    use HuaweiCloud\SDK\Core\Exceptions\RequestTimeoutException;
    use HuaweiCloud\SDK\Core\Exceptions\ServiceResponseException;
    use HuaweiCloud\SDK\IoTDA\V5\IoTDAClient;
    // There will be security risks if the AK/SK used for authentication is directly written into code. Encrypt the AK/SK in the configuration file or environment variables for storage;
    // In this example, the AK/SK stored in the environment variables are used. Configure the environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK in the local environment first.
    $ak = getenv('HUAWEICLOUD_SDK_AK');
    $sk = getenv('HUAWEICLOUD_SDK_SK');
    // endpoint: On the console, choose Overview and click Access Addresses to view the HTTPS application access address.
    // $endpoint = "https://iotda.cn-north-4.myhuaweicloud.com";
    $endpoint = "<YOUR ENDPOINT>";
    $projectId = "<YOUR PROJECT_ID>";
    // Create a credential.
    $credentials = new BasicCredentials($ak,$sk,$projectId);
    // Modify the default configuration and skip the server certificate verification.
    $config = HttpConfig::getDefaultConfig();
    $config->setIgnoreSslVerification(true);
    // Create an IoTDAClient instance and initialize it. (If the default configuration is not modified, you do not need to add config.)
    $client = IoTDAClient::newBuilder(new IoTDAClient)
      ->withHttpConfig($config)
      ->withEndpoint($endpoint)
      ->withCredentials($credentials)
      ->build();
    // Instantiate a request object.
    $request = new ListDevicesRequest();
    try {
    // Call the API for querying the device list.
      $response = $client->ListDevices($request);
    } catch (ConnectionException $e) {
      $msg = $e->getMessage();
      echo "\n". $msg ."\n";
    } catch (RequestTimeoutException $e) {
      $msg = $e->getMessage();
      echo "\n". $msg ."\n";
    } catch (ServiceResponseException $e) {
      echo "\n";
      echo $e->getHttpStatusCode(). "\n";
      echo $e->getErrorCode() . "\n";
      echo $e->getErrorMsg() . "\n";
    }
    echo "\n";
    echo $response;

    Parameter

    Description

    ak

    Access key ID of your Huawei Cloud account. You can create and view an AK/SK on the My Credentials > Access Keys page of the Huawei Cloud management console. For more information, see Access Keys.

    sk

    Secret access key (SK) of your Huawei Cloud account.

    endpoint

    Endpoint of the region where the Huawei Cloud service to be accessed is located.

    On the console, you can view the region name of the current service and the mapping between regions and endpoints. For details, see Platform Connection Information.

    projectId

    ID of the project where the Huawei Cloud service to be accessed is located. Select a project ID based on the region to which the project belongs.

Additional Information

For details on the project source code and usage guide, see Huawei Cloud PHP Software Development Kit (PHP SDK).