Learn how to install, configure, and use IoT Platform SDK for PHP to call IoT Platform API operations.
Install the SDK
IoT Platform SDK for PHP is part of Alibaba Cloud SDK for PHP. If you already installed Alibaba Cloud SDK for PHP, skip this section.
-
Install a PHP development environment.
PHP 5.5.0 or later is required. Download and install PHP from the official PHP website.
-
Install Composer.
Composer manages the IoT Platform SDK for PHP dependency.
-
On Windows, download and run Composer-Setup.exe from getcomposer.org.
-
Alternatively, run the following curl command:
curl -sS https://getcomposer.org/installer | php
NoteIf installation fails due to network issues, use the Alibaba Cloud Composer mirror.
-
-
Add the IoT Platform SDK for PHP dependency:
composer require alibabacloud/iotSource code and documentation are available in the openapi-sdk-php-iot and Alibaba Cloud SDK for PHP GitHub repositories.
Initialize the SDK
The following code shows an example of how to initialize the SDK to call an API in the China (Shanghai) region.
Create a $iClientProfile object with your region and credentials, then pass it to DefaultAcsClient($iClientProfile) to initialize the client.
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 |
Stores the SDK initialization information. View your region in the upper-left corner of the IoT Platform console. All available region IDs are listed in Supported regions. |
Initiate a request
Each API operation has a corresponding class named ${API operation name}+"Request". Create a $request instance, set parameters with "set"+${Request parameter} methods, and call getAcsResponse($request) to get the response.
For a list of IoT Platform cloud APIs, see API list. For descriptions of the request parameters in `request` and the response parameters in `response`, see the corresponding API reference.
This topic uses the Pub operation as an example to show how to publish a message to a topic. For information about the request parameters, see Pub.
In the following code, ${iotInstanceId} represents the instance ID. You can find the ID of the current instance on the Instance Overview page in the IoT Platform console.
If an ID exists, you must pass this ID. Otherwise, the API call fails.
If the Instance Details page or the ID does not exist, you do not need to pass an ID. You can delete the request code related to `IotInstanceId` or pass an empty value `""`. Otherwise, the API call fails.
For more information about instances, see Instance overview. For information about how to purchase an instance, see Purchase an Enterprise instance. For frequently asked questions, 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 go to the IoT Platform Cloud SDK Example Center to view or download sample code for API calls. The sample code provides examples for the Java, Python, PHP, .NET, and Go SDKs.
The Alibaba Cloud OpenAPI Developer Portal provides an online API debugging tool. On the API debugging page, you can quickly search for and test API calls. The system automatically generates sample SDK code for different languages based on the parameters that you enter. The sample code is displayed on the SDK Example tab on the right side of the page. On the Call Result tab, you can view the request URL and the response in JSON format.