Created
September 28, 2010 21:29
-
-
Save sandaru1/601818 to your computer and use it in GitHub Desktop.
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 | |
class Notifo_API { | |
const API_ROOT = 'https://api.notifo.com/'; | |
const API_VER = 'v1'; | |
protected $apiUsername; | |
protected $apiSecret; | |
/** | |
* class constructor | |
*/ | |
function __construct($apiUsername, $apiSecret) { | |
$this->apiUsername = $apiUsername; | |
$this->apiSecret = $apiSecret; | |
} | |
function set_apiusername($val) { | |
$this->apiUsername = $val; | |
} | |
function set_apisecret($val) { | |
$this->apiSecret = $val; | |
} | |
/** | |
* function: sendNotification | |
* @param: $params - an associative array of parameters to send to the Notifo API. | |
* These can be any of the following: | |
* to, msg, label, title, uri | |
* See https://api.notifo.com/ for more information | |
*/ | |
function sendNotification($params) { | |
$validFields = array('to', 'msg', 'label', 'title', 'uri'); | |
$params = array_intersect_key($params, array_flip($validFields)); | |
return $this->sendRequest('send_notification', 'POST', $params); | |
} /* end function sendNotification */ | |
/** | |
* function: subscribeUser | |
* @param: $username - the username to subscribe to your Notifo service | |
* See https://api.notifo.com/ for more information | |
*/ | |
function subscribeUser($username) { | |
return $this->sendRequest('subscribe_user', 'POST', array('username' => $username)); | |
} /* end function subscribeUser */ | |
/** | |
* helper function to send the requests | |
* @param $method - name of remote method to call | |
* @param $type - HTTP method (GET, POST, etc) | |
* @param $data - array with arguments for remote method | |
*/ | |
function sendRequest($method, $type, $data) { | |
$url = self::API_ROOT.self::API_VER.'/'.$method; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
if ($type == "POST") { | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
} | |
curl_setopt($ch, CURLOPT_USERPWD, $this->apiUsername.':'.$this->apiSecret); | |
curl_setopt($ch, CURLOPT_HEADER, false); | |
/* | |
* if you are on a shared host or do not have access to install | |
* the root CA certificates on your server, uncomment the next | |
* two lines or the curl_exec call may fail with null | |
*/ | |
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); | |
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); | |
$result = curl_exec($ch); | |
$result = json_decode($result, true); | |
return $result; | |
} /* end function sendRequest */ | |
// for backwards compatibility | |
function send_notification($params) { return json_encode($this->sendNotification($params)); } | |
function subscribe_user($username) { return json_encode($this->subscribeUser($username)); } | |
function send_request($url, $type, $data) { return json_encode($this->sendRequest($method, $type, $data)); } | |
} /* end class Notifo_API */ | |
?> |
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 | |
require("notifo.php"); | |
$url = "http://stream.twitter.com/1/statuses/filter.json"; | |
$twitter_username = "your_username"; | |
$twitter_password = "your_password"; | |
$notifo_username = "notifo_username"; | |
$notifo_apikey = "notifo_api_key_from_http://notifo.com/user/settings"; | |
$notifo = new Notifo_API($notifo_username,$notifo_apikey); | |
$buffer = ""; | |
function process_tweet($json) { | |
global $notifo,$notifo_username; | |
$tweet = json_decode($json); | |
if (!is_object($tweet)) return; | |
$notifo->sendNotification(array('to' => $notifo_username,'msg' => $tweet->user->screen_name.' : '.$tweet->text,'title' => $tweet->user->screen_name,'label' => 'twitter','uri' => 'http://twitter.com/'.$tweet->user->screen_name.'/status/'.$tweet->id)); | |
echo $tweet->text."\n"; | |
} | |
function process_data($curl, $data) | |
{ | |
global $buffer; | |
$str = $buffer.$data; | |
$lines = split("\n",$str); | |
for($i=0;$i<count($lines)-1;$i++) { | |
process_tweet($lines[$i]); | |
} | |
if ($data[strlen($data)-1]=="\n") { | |
process_tweet($lines[count($lines)-1]); | |
$buffer = ""; | |
} else { | |
$buffer = $lines[count($lines)-1]; | |
} | |
return strlen($data); | |
} | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, 'track=#tco10'); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_USERPWD, $twitter_username.':'.$twitter_password); | |
curl_setopt($curl, CURLOPT_WRITEFUNCTION, "process_data"); | |
curl_exec($curl); | |
curl_close($curl); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment