Last active
August 29, 2015 14:16
-
-
Save harrisonhjones/1990668b32ae68e4519e to your computer and use it in GitHub Desktop.
Spark PHP Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo "Program Start<br/>"; | |
/* Desired Curl Command | |
curl https://api.spark.io/v1/devices/0123456789abcdef/brew \ | |
-d access_token=123412341234 \ | |
-d "args=coffee" | |
*/ | |
// From http://davidwalsh.name/curl-post | |
//set POST variables | |
$accessToken = "XXX"; | |
$deviceID = "YYY"; | |
$deviceFunction = "led"; | |
$argument = "D7,HIGH"; | |
$url = 'https://api.spark.io/v1/devices/' . $deviceID . '/' + $deviceFunction ; | |
$fields = array( | |
'access_token' => urlencode($accessToken), | |
'args' => urlencode($argument) | |
); | |
//url-ify the data for the POST | |
$fields_string = ""; | |
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } | |
$fields_string = rtrim($fields_string, '&'); | |
//open connection | |
$ch = curl_init(); | |
//set the url, number of POST vars, POST data | |
curl_setopt($ch,CURLOPT_URL, $url); | |
curl_setopt($ch,CURLOPT_POST, count($fields)); | |
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); | |
// Disable SSL verification | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
// Will return the response, if false it print the response | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
//execute post | |
echo "Executing Curl Operation<br/>"; | |
$result = curl_exec($ch); | |
echo "Curl Result: '" . $result . "'<br/>"; | |
//close connection | |
curl_close($ch); | |
echo "Program End<br/>"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment