Quick and Dirty PHP sample program to send a data to feed

101 views
Skip to first unread message

Christophe

unread,
May 11, 2011, 11:03:47 AM5/11/11
to Open.Sen.se Forum
Hello,

I just received my invitation, here a (very) Quick&Dirty PHP code to
try my Sen.se account before add Sen.se features in my device.

To try, just change $feedId and $senseKey.

<?php
/**
* sensePublishingEvent : send data to my feed
* Quick&Dirty...
* Don't forget to activate Curl
* @version 1.0
* @author Christophe Courche
*/

//Need to be change before use.
$host='api.sen.se';
$feedId = 'HereYourFeedID';
$data = '10';
$senseKey = 'HereYourSenseKey';
$posturl= "/events/?sense_key=$senseKey"; //$posturl= "/events/";
$paramsToPostInJSON = '
{
"feed_id": '.$feedId.',
"value": '.$data.'
}';

$header = array(
"POST $posturl HTTP/1.0",
"Host: $host",
"User-Agent: T2NET",
"Content-Type: application/json",
//"sense_key = \"$senseKey\"", //not work, need to investigate
more
"Connection: close"
);

$url = "http://".$host.$posturl;

echo "<h3>sensePublishingEvent !</h3></br>";

//Show the header
echo "<b>Header :</b></br>";
print_r ($header);
//Show the Url
echo "<br/><b>Url :</b></br>".$url."</br>";
//Show the JSON
echo "<br/><b>paramsToPostInJSON :</b></br>".$paramsToPostInJSON."</
br>";

//Ready ?
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_PROXY, '10.193.118.30:3128'); //if need !
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $paramsToPostInJSON);
//Let's go !
$result = curl_exec($ch);
curl_close($ch);

//And...
echo "<br/><b>Sen.se answer :</b></br>".$result."</br>";

//Thank you Sen.se
?>

Reply all
Reply to author
Forward
0 new messages