IoT Platform provides developers with an SDK for PHP. This article describes how to install and configure IoT Platform SDK for PHP. This article also describes how to use the SDK to call the 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.
Install PHP.
PHP 5.5.0 and later are supported. Download a PHP installation package from the official PHP website and install PHP.
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
NoteIf the installation fails due to network issues, you can use the full image of Alibaba Cloud Composer.
Add the following dependency to install IoT Platform SDK for PHP:
composer require alibabacloud/iot
For more information, see IoT Platform SDK for PHP 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:
<?php
include_once 'aliyun-php-sdk-core/Config.php';
use \Iot\Request\V20180120 as Iot;
// Set your AccessKey ID, AccessKey secret, and ProductKey.
$accessKeyId = "";
$accessSecret = "";
$iClientProfile = DefaultProfile::getProfile("cn-shanghai", $accessKeyId, $accessSecret);
$client = new DefaultAcsClient($iClientProfile);
Parameter | Description |
$accessKeyId | The AccessKey ID of your Alibaba Cloud account. You can go to the User Management console to create or view your AccessKey ID. |
$accessSecret | The AccessKey secret of your Alibaba Cloud account. |
$iClientProfile | The object that is used to store the SDK initialization information. You can view the region in the upper-left corner of the IoT Platform console. For more information about region IDs, see Regions and zones. |
Initiate a request
${API operation name}+"Request"
and ${API operation name}+"Response"
formats. ${API operation name}+"Request"
: This class is used to call an API operation. You can use therequest
instance of this class and call theset +${request parameters}
method to configure the request parameters.${API operation name}+"Response"
: You can call thegetAcsResponse(request)
method of the client instance that is created from DefaultAcsClient to obtain a response. You can use theresponse
instance of the ${API operation name}+"Response" class and call theget +${response parameters}
method to obtain the values of the response parameters.For example, you can call the
response.getSuccess()
method to obtain the Success parameter. This parameter is a common response parameter that indicates whether a call is successful. Common response parameters also include the RequestId, ErrorMessage, and Code parameters.
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.
$request = new Iot\PubRequest();
$request->setProductKey("productKey");
$request->setMessageContent("aGVsbG93b3JsZA="); //hello world Base64 String.
$request->setTopicFullName("/productKey/deviceName/get"); // The full name of the topic to which the message is sent.
$response = $client->getAcsResponse($request);
print_r($response);
Appendix: Sample code
Download the IoT Platform SDK sample code. The sample code of the SDKs for Java, Python, PHP, .NET, and Go is provided.
Alibaba Cloud provides OpenAPI Explorer to simplify API usage. You can use OpenAPI Explorer to search for API operations, call API operations, and dynamically generate SDK sample code. OpenAPI Explorer dynamically generates the sample code of the operation for different SDKs. On the right side of the page, you can view the sample code of an SDK on the Example Code tab. On the Debugging Result tab, you can view the actual request URL and response in the JSON format.