PHP consult/assing values


#1

Hello guys, I’ve done something interesting with thinger.io platform and PHP

I’ve consulted by a PHP page directly the thinger.io platform, here is the code that I used

<style type="text/css">
	body {
	    color: #555;
	    font-size: 14px;
	    font-family: "Helvetica", "Arial", sans-serif;
        padding: 80px;			
	}	

	title {
	    color: #555;
	    font-size: 20px;
	    font-family: "Helvetica", "Arial", sans-serif;
        padding: 80px;			
	}


	p.section {
		margin-top: 50px;
	}

	.button {
		color: #fff;
	    background-color: #428bca;
	    border-color: #357ebd;
	    border-radius: 5px;   
	    width: 70px;
    	height: 30px;
	}

	.button:hover {
		color: #428bca;
	    background-color: #fff;
	}



</style>


<?php
	$device = 'Galeria'; // the device id
	$user = 'ega'; // your user id
	$resource = 'Uptime'; 

	$resource12 = 'Time2'; 
 	$resource1 = 'Time';
 	$resource22 = 'Flow2'; 
 	$resource2 = 'Flow';
 	$resource32 = 'Volume2';
 	$resource3 = 'Volume';
	$authorization = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkZXYiOiJHYWxlcmlhIiwiaWF0IjoxNDk0ODc2NDAwLCJqdGkiOiI1OTFhMDBmMDY0MmE3NjM0OGM3YjkzNTIiLCJ1c3IiOiJlZ2EifQ.WYWAkVzwRRsk7s2cAnyScbimD5LwFRaZLW4o2RFUX7s'; // a device access token for controlling all or this resource only

///UPTIME

	echo("<p class='title'>Demo");
	echo "</p>";

    $url = "https://api.thinger.io/v2/users/{$user}/devices/{$device}/{$resource}?authorization={$authorization}";
	$ch = curl_init();
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	$contents = curl_exec($ch);

	if (curl_errno($ch)) {
		echo curl_error($ch);
		echo "\n<br />";
		$contents = '';
	} else {
		curl_close($ch);
	}

	if (!is_string($contents) || !strlen($contents)) {
		echo "Failed to get contents.";
		$contents = '';
	}

	echo("<p class='section'>The uptime is: ");
	echo substr($contents,7,-1);
	echo "</p>";

///////////// FIRST CONSULT
	$url = "https://api.thinger.io/v2/users/{$user}/devices/{$device}/{$resource12}?authorization={$authorization}";
	$ch = curl_init();
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	$contents = curl_exec($ch);

	if (curl_errno($ch)) {
		echo curl_error($ch);
		echo "\n<br />";
		$contents = '';
	} else {
		curl_close($ch);
	}

	if (!is_string($contents) || !strlen($contents)) {
		echo "Failed to get contents.";
		$contents = '';
	}


	$inVariable1 = substr($contents,7,-1);
	
	if ($_POST['inVariable1']) {
		$inVariable1 = $_POST['inVariable1'];
	}
///////////// SECOND CONSULT
	$url = "https://api.thinger.io/v2/users/{$user}/devices/{$device}/{$resource22}?authorization={$authorization}";
	$ch = curl_init();
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	$contents = curl_exec($ch);

	if (curl_errno($ch)) {
		echo curl_error($ch);
		echo "\n<br />";
		$contents = '';
	} else {
		curl_close($ch);
	}

	if (!is_string($contents) || !strlen($contents)) {
		echo "Failed to get contents.";
		$contents = '';
	}


	$inVariable2 = substr($contents,7,-1);
	
	if ($_POST['inVariable2']) {
		$inVariable2 = $_POST['inVariable2'];
	}	
///////////// THIRD CONSULT
	$url = "https://api.thinger.io/v2/users/{$user}/devices/{$device}/{$resource32}?authorization={$authorization}";
	$ch = curl_init();
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	$contents = curl_exec($ch);

	if (curl_errno($ch)) {
		echo curl_error($ch);
		echo "\n<br />";
		$contents = '';
	} else {
		curl_close($ch);
	}

	if (!is_string($contents) || !strlen($contents)) {
		echo "Failed to get contents.";
		$contents = '';
	}


	$inVariable3 = substr($contents,7,-1);
	
	if ($_POST['inVariable3']) {
		$inVariable3 = $_POST['inVariable3'];
	}	
?>
		<form action="" method="post">
	   		<p>
	   			Time .....  (s) : <input type="text" name="inVariable1" value="<?php echo $inVariable1; ?>"/>
				<input type="submit" name="submit" value="Submit" class="button" />
			</p>
		</form>
		<form action="" method="post">
	   		<p>
	   			  Flow  ... (L/s): <input type="text" name="inVariable2" value="<?php echo $inVariable2; ?>"/>
				<input type="submit" name="submit" value="Submit" class="button" />
			</p>
		</form>
		<form action="" method="post">
	   		<p>
	   			Volumen (L) : <input type="text" name="inVariable3" value="<?php echo $inVariable3; ?>"/>
				<input type="submit" name="submit" value="Submit" class="button" />
			</p>
		</form>

<?php
////ASSING VALUE 1
	$data = array('in' => intval($inVariable1));
	$data_string = json_encode($data);
	$url = "https://api.thinger.io/v2/users/{$user}/devices/{$device}/{$resource1}?authorization={$authorization}";
	$ch = curl_init();
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
	curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
		'Content-Type: application/json',                                                                                
		'Content-Length: ' . strlen($data_string))                                                                       
	);                                                                                                                   
																														 
	$result = curl_exec($ch);
//	echo("<p class='section'>Result value : ");
//	echo $result;
//	echo "</p>";

	$contents = curl_exec($ch);
	if (curl_errno($ch)) {
		echo curl_error($ch);
		echo "\n<br />";
		$contents = '';
	} else {
		curl_close($ch);
	}

	if (!is_string($contents) || !strlen($contents)) {
		echo "Failed to get contents.";
		$contents = '';
	}


////ASSING VALUE 2
	$data = array('in' => intval($inVariable2));
	$data_string = json_encode($data);
	$url = "https://api.thinger.io/v2/users/{$user}/devices/{$device}/{$resource2}?authorization={$authorization}";
	$ch = curl_init();
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
	curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
		'Content-Type: application/json',                                                                                
		'Content-Length: ' . strlen($data_string))                                                                       
	);                                                                                                                   
																														 
	$result = curl_exec($ch);
//	echo("<p class='section'>Result value : ");
//	echo $result;
//	echo "</p>";

	$contents = curl_exec($ch);
	if (curl_errno($ch)) {
		echo curl_error($ch);
		echo "\n<br />";
		$contents = '';
	} else {
		curl_close($ch);
	}

	if (!is_string($contents) || !strlen($contents)) {
		echo "Failed to get contents.";
		$contents = '';
	}



////ASSING VALUE 3
	$data = array('in' => intval($inVariable3));
	$data_string = json_encode($data);
	$url = "https://api.thinger.io/v2/users/{$user}/devices/{$device}/{$resource3}?authorization={$authorization}";
	$ch = curl_init();
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
	curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
		'Content-Type: application/json',                                                                                
		'Content-Length: ' . strlen($data_string))                                                                       
	);                                                                                                                   
																														 
	$result = curl_exec($ch);
//	echo("<p class='section'>Result value : ");
//	echo $result;
//	echo "</p>";

	$contents = curl_exec($ch);
	if (curl_errno($ch)) {
		echo curl_error($ch);
		echo "\n<br />";
		$contents = '';
	} else {
		curl_close($ch);
	}

	if (!is_string($contents) || !strlen($contents)) {
		echo "Failed to get contents.";
		$contents = '';
	}

																			   
																														 



?>

I didnt figure it out how to extract the value directly of the variable, so I had to make 2 variables, one as output, and put into the input field, and other as input, and send it by the webpage (and just make both equal in microcontroller’s code), when I refer to “input” and “output” I’m talking from the position of the microcontroller, and thinger.io variables.

Go ahead with faith :wink: