CloudPlugs REST Library  1.0.0
for PHP
CloudPlugs PHP Client Library

CloudPlugs REST Client for PHP is a library to perform HTTP requests to CloudPlugs servers. The official repository is: https://github.com/cloudplugs/rest-php

The Cloudplugs IoT Platform

Cloudplugs is a cloud based IoT platform to enable the fast prototyping, connection, deployment and management of smart "Things" (sensors, smartphones, home appliances, etc.). Different objects can interact with each other by publishing and/or reading any type of data on shared channels through simple communication protocols. You can learn more at http://www.cloudplugs.com/how-it-works/ and you can sign-up on http://www.cloudplugs.com/register to start connecting your Things.

Library Guide

Installation

Just include the library in your PHP script:

include_once 'RestClient.class.php';

Usage

Use the Clouplugs platform's namespace:

Create a new instance:

$client = new RestClient();

Now you are ready to configure the library and invoke methods to perform REST requests to the server!

Configuration

After you have a new instance of the class, there are a few configuration parameters that you can customize, if needed, before invoking any request. These parameters are:

  • "outputFormat": to force the library to format server's response in a specific data format
  • "debugMode": to display debug information
  • "timeout": to set the timeout value for HTTP requests
  • "id": to set the authentication identifier
  • "pass": to set the authentication password
  • "isMaster": to run the script as a master user
  • "baseUrl": to set the base URL for HTTP requests
  • "enableSSL": to switch SSL mode on/off

You can set these options using the configuration methods as in the following example:

$client->setDebugMode(TRUE);
$client->setTimeout(50);

Or using the setOptions() method:

$options = array('debugMode' => TRUE, 'timeout' => 50);
$res = $client->setOptions($options);

Security

To enhance security, the library is configured to run over HTTPS by default. To establish an SSL connection with the Cloudplugs Platform, Cloudplugs's certificate (named 'cacert.pem') must be always present in the same directory of the library. You can choose whether to switch SSL mode on or off at any time by invoking the method enableSSL(bool). Example:

$client->enableSSL(TRUE); //switches to HTTPS
$client->enableSSL(FALSE); //switches to HTTP

Even if Cloudplugs supports both HTTP and HTTPS, is strongly recommended to always run the library over HTTPS.

Executing REST HTTP requests

When your instance of the library is ready, you can execute HTTP requests to the server by invoking any of the request methods. All the requests are synchronous and are performed according to the Cloudplugs REST API documentation. Example:

//read all device properties and information
$res = $client->getDevice();
//set a custom device property
$res = $client->setDeviceProp(NULL, 'myPropertyName', 123);