Updated on 2024-10-31 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.

Obtaining and Installing the SDK

  1. Install the PHP development environment.

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

    Huawei Cloud PHP SDKs support PHP 5.6, PHP 6, and PHP 7, but does not support PHP 8. Before running PHP SDKs, run the php --version command to check the current PHP version. If PHP of other languages is installed, an error may occur when you run PHP SDKs.

  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\Auth\Credentials;
    use HuaweiCloud\SDK\Core\Exceptions\ConnectionException;
    use HuaweiCloud\SDK\Core\Exceptions\RequestTimeoutException;
    use HuaweiCloud\SDK\Core\Exceptions\ServiceResponseException;
    use HuaweiCloud\SDK\Core\Region\Region;
    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>";
    // REGION_ID: If CN East-Shanghai1 is used, enter cn-east-3. If CN North-Beijing4 is used, enter cn-north-4. If CN South-Guangzhou is used, enter cn-south-1.
    $regionId = "<YOUR REGION ID>";
    // Create a credential.
    $credential = new BasicCredentials($ak,$sk,$projectId);
    // withDerivedPredicate is used for the standard or enterprise edition. For the basic edition, delete the line.
    $credential->withDerivedPredicate(Credentials::getDefaultDerivedPredicate());
    // 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($credential)
      ->withRegion(new Region($regionId, $endpoint))
      ->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 details, 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).