I am trying to set data in a device using API with PHP. The following code works fine and sets the value of the resource (‘starter’) to 1 when called.
<?php
$device = 'Mydevice'; // the device id
$user = 'user1'; // your user id
$resource = 'starter'; // your resource name like 'led', 'relay', and so on.
$authorization = 'authToken'; // a device access token for controlling all or this resource only
$go = 1; // setting value 1 for this counter
$url = "https://api.thinger.io/v2/users/{$user}/devices/{$device}/{$resource}?authorization={$authorization}";
$data = array(
'in' => $go
);
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $data ),
'header'=> "Content-Type: application/json\r\n" .
"Accept: application/json\r\n"
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );
?>
However, I am unable to set more than 1 value in a resource.
example:
esp8266 is sending 2 sensors data as:
thing["usage"] >> [](pson & out) {
out["Wh1"] = Wh1;
out["Wh2"] = Wh2;
};
I tried the PHP code below to change the values of “Wh1” and “Wh2” but it does not work. Any advice is appreciated.
<?php
$device = 'Mydevice'; // the device id
$user = 'user1'; // your user id
$resource = 'usage'; // your resource name like 'led', 'relay', and so on.
$authorization = 'authToken'; // a device access token for controlling all or this resource only
$uso = array('Wh1'=> 8,'Wh2' => 99); // trying to set 2 values for this counter
$url = "https://api.thinger.io/v2/users/{$user}/devices/{$device}/{$resource}?authorization={$authorization}";
$data = array(
'in' => $uso
);
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $data ),
'header'=> "Content-Type: application/json\r\n" .
"Accept: application/json\r\n"
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );
?>