We have prepared a PHP sample program to illustrate how to use our open API.
Use this sample in a PHP operable environment and obtain and enter the proper authentication info to run an operational check of the API.
<?php
/**
* T&D sample program for using open API
*
* API User’s Guide http://www.webstorage-service.com/docs/api/
*
*/
// Use device/current to retrieve a list of current readings
$url = "https://api.webstorage-service.com/v1/devices/current";
//HTTP header field to be set: Please use according to API specifications.
$headerdata = array(
'Content-Type: application/json',
'X-Http-Method-Override: GET'
);
//create a temporary array using the data sent via POST :For details about the sent data, see the API Specifications.
//*Note that the created array will be changed to JSON format by using PHP standard functions to generate a JSON-formatted character array.
$param = array(
"api-key" => "apikeyapikeyapikeyapikeyapikeyapikey" // Enter the appropriate API Key.
,"login-id" => "your-login-id" // Enter User ID
,"login-pass" => "your-login-password" // Enter the correct password for the entered User ID.
//parameters can be added as necessary
);
//the outgoing data will be changed from the array to JSON format.
$postdata = json_encode($param);
//HTTP POST send processing using curl
$ch = curl_init($url);
$options = array (
CURLOPT_POST => true //send using HTTP "POST"
,CURLOPT_RETURNTRANSFER => true //stringize the curl_exec return value
,CURLOPT_HEADER => true //output the header contents:necessary if checking the HTTP status code & rate limit
,CURLOPT_HTTPHEADER => $headerdata //array for HTTP header field to be set
,CURLOPT_POSTFIELDS => $postdata //data to be sent using HTTP "POST"
);
curl_setopt_array($ch, $options);
//receive results, close communication with curl
$response = curl_exec($ch); //store results as character string
$response_info = curl_getinfo($ch); //store info related to results
$response_code = $response_info['http_code']; //HTTP status code for communication result
$response_header_size = $response_info['header_size']; //header size for communication result
curl_close($ch); //close communication
//confirm the details of the header info because within the response header info is also included rate limit info,
$response_header = substr($response, 0, $response_header_size); // clip the header
$response_body = substr($response, $response_header_size); //clip the body
//check the header
$array_header = decodeHeader($response_header); //disassemble the header
//X-Ratelimit-Limit
if ( isset($array_header["X-RateLimit-Limit"]) ) {
$ratelimit["Limit"] = $array_header["X-RateLimit-Limit"];
}else{
$ratelimit["Limit"] = "unknown";
}
//X-Ratelimit-Reset
if ( isset($array_header["X-RateLimit-Reset"]) ) {
$ratelimit["Reset"] = $array_header["X-RateLimit-Reset"];
}else{
$ratelimit["Reset"] = "unknown";
}
//X-Ratelimit-Remaining
if ( isset($array_header["X-RateLimit-Remaining"]) ) {
$ratelimit["Remaining"] = $array_header["X-RateLimit-Remaining"];
}else{
$ratelimit["Remaining"] = "unknown"; }
//X-Ratelimit-Remaining-DataCount
if ( isset($array_header["X-RateLimit-Remaining-DataCount"]) ) {
$ratelimit["DataCount"] = $array_header["X-RateLimit-Remaining-DataCount"];
}else{
$ratelimit["DataCount"] = ""; }
//display processing results
if ( $response_code == 200 ) {
//communication successful
print "[Result] success.\n";
}else{
print "[Result] failed [$response_code].\n";
}
if ( $ratelimit["DataCount"]=="" ) {
//if no limit on number of data readings
print "[X-RateLimit-Limit] ".$ratelimit["Limit"]."\n";
print "[X-RateLimit-Reset] ".$ratelimit["Reset"]."\n";
print "[X-RateLimit-Remaining] ".$ratelimit["Remaining"]."\n";
}else{
//if there is a limit on the number of data readings
print "[X-RateLimit-Limit] ".$ratelimit["Limit"]."\n";
print "[X-RateLimit-Reset] ".$ratelimit["Reset"]."\n";
print "[X-RateLimit-Remaining] ".$ratelimit["Remaining"]."\n";
print "[X-RateLimit-Remaining-DataCount] ".$ratelimit["DataCount"]."\n";
}
//process the JSON data in body
print "[ResponseData]\n".trim($response_body);
exit();
/**
*disassemble the header line by line, divide with a colon and store in an array
*
* @param string $header
* @return array
*/
function decodeHeader($header){
//process in one line units, and treat ":" as a delimiter
$result = array();
foreach (explode("\n", $header) as $i=>$line) {
$temp = explode(":",$line);
$temp = array_map('trim',$temp); //trim each element
if ( isset($temp[0]) and isset($temp[1]) ){
// process only the data separated by ":"
$result[$temp[0]] = $temp[1];
}
}
return $result;
}