All Products
Search
Document Center

IoT Platform:Use IoT Platform SDK for PHP

Last Updated:Oct 22, 2023

IoT Platform provides an SDK for PHP. This topic describes how to install and configure IoT Platform SDK for PHP. This topic also provides sample code on how to use the SDK to call API operations of IoT Platform.

Install the SDK

IoT Platform SDK for PHP is part of Alibaba Cloud SDK for PHP. If you have installed Alibaba Cloud SDK for PHP, you do not need to install IoT Platform SDK for PHP.

  1. Install a PHP development environment.

    PHP 5.5.0 and later are supported. Download a PHP installation package from the official PHP website and install PHP.

  2. Install Composer.

    Composer is used to manage IoT Platform SDK for PHP. Therefore, you must install Composer in your system.

    • If you use Windows, you can download the Composer-Setup.exe program from the getcomposer.org website and install Composer.

    • You can also run the following curl command to install Composer:

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

    If the installation fails due to network exceptions, you can use the full image of Alibaba Cloud Composer.

  3. Add the following dependency to install IoT Platform SDK for PHP:

    composer require alibabacloud/iot

    For more information, visit openapi-sdk-php-iot and Alibaba Cloud SDK for PHP.

Initialize the SDK

The following example shows how to initialize the SDK when IoT Platform is activated in the China (Shanghai) region:

Create a $iClientProfile object to store SDK initialization information. Then, create a $client instance of the DefaultAcsClient class. Call the DefaultAcsClient($iClientProfile) method to load the SDK initialization information.

include_once 'aliyun-php-sdk-core/Config.php';
use \Iot\Request\V20180120 as Iot;
// Set your AccessKey ID, AccessKey secret, and ProductKey.
$accessKeyId = getenv('ACCESS_KEY_ID');
$accessSecret = getenv('ACCESS_KEY_SECRET');
$iClientProfile = DefaultProfile::getProfile("cn-shanghai", $accessKeyId, $accessSecret);
$client = new DefaultAcsClient($iClientProfile);

Parameter

Description

$iClientProfile

The object that is used to store the SDK initialization information. cn-shanghai indicates the ID of the region in which your IoT Platform service resides.

You can view the region in the upper-left corner of the IoT Platform console.

For more information about region IDs, see Supported regions.

Initiate a request

The SDK encapsulates a class for each API operation. The class name is in the ${API operation name}+"Request" format. You can use the class to initiate API requests. You can create a $request instance of this class and call the "set"+${Request parameter} method to specify the request parameters. Then, you can call the getAcsResponse($request) method of the $client instance to obtain a response.

For more information about the API operations of IoT Platform, see List of operations by function. For more information about the request and response parameters of each API operation, see the API documentation.

The following example shows how to call the Pub operation to publish a message to a topic. For more information about request parameters, see Pub.

Important

In the following sample code, ${iotInstanceId} specifies the ID of an instance. You can view the ID of the instance on the Overview page in the IoT Platform console.

  • If your instance has an ID, you must specify the ID for this parameter. Otherwise, the call fails.

  • If no Overview page or ID is generated for your instance, you do not need to configure this parameter. You must delete the request code that is related to the IotInstanceId parameter or specify an empty string ("") for the parameter. Otherwise, the call fails.

For more information about IoT Platform instances, see Overview. For information about how to purchase an instance, see Purchase Enterprise Edition instances. For more information, see FAQ about IoT Platform instances.

$request = new Iot\PubRequest();
$request->setIotInstanceId("iotInstanceId"); 
$request->setProductKey("productKey");
$request->setMessageContent("aGVsbG93b3JsZA="); //hello world Base64 String.
$request->setTopicFullName("/productKey/deviceName/get"); // The full name of the topic that is used to publish the message.
$response = $client->getAcsResponse($request);
print_r($response);

Appendix: Sample code

You can view or download the sample code of API operations in IoT Platform SDK Sample Center. Sample code of SDKs for Java, Python, PHP, .NET, and Go is provided.

Alibaba Cloud OpenAPI Explorer provides online debugging tools for API operations. On the API Debugging page, you can search for API operations, call API operations, and generate sample code for API operations of different SDKs. On the right side of the page, you can view the sample code of an SDK on the Sample Code tab. On the Debugging Result tab, you can view the actual request URL and response in the JSON format.