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 API Reference on the Application Side.
SDK Obtaining and Installing
- Install the PHP development environment.
Visit the PHP website, and download and install the PHP development environment.
Huawei Cloud PHP SDK supports PHP 5.6, PHP 7, and PHP 8. Before running the PHP SDK, run the php --version command to check the PHP version. If other PHP versions are installed, an error may be reported when you run the PHP SDK.
- Install Composer.
curl -sS https://getcomposer.org/installer | php
- Install the PHP SDK.
composer require huaweicloud/huaweicloud-sdk-php
- Import the auto loading file of Composer.
require 'path/to/vendor/autoload.php';
Sample Code
The following code sample shows how to use the PHP SDK to call the API for Querying the Device List.
- Create a credential.
- Create and initialize an IoTDAClient instance.
- Instantiate a request object.
- 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, and decrypt the AK/SK when using them; // 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 (AK) of your Huawei Cloud account. You can create and view an AK/SK on the page of the Huawei Cloud console. For details, see Access Keys.
sk
Secret access key (SK) of your Huawei Cloud account.
endpoint
On the console, choose Overview and click Access Addresses to view the HTTPS application access address.
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.
regionId
Replace it with the region where you want to access the IoT platform. For details about the mapping between regions and endpoints, see Regions and Endpoints.
Additional Information
For details on the project source code and usage guide, see Huawei Cloud PHP Software Development Kit (PHP SDK).
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot