Created
February 15, 2011 19:09
-
-
Save yahelc/828029 to your computer and use it in GitHub Desktop.
Searches for a hashtag in a set list of users' timelines, and SMSes the contents to a number using Twilio API. Uses basic MySQL store for preventing duplicate SMSes. Uses fancy tweet entities for hashtag detection.
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 | |
$link = mysql_connect('localhost', 'username', 'password'); | |
mysql_select_db('database_name'); | |
$users = array('user_a', 'user_b', 'user_c', 'user_d'); //twitter handles, must be public timelines | |
$post_array = array('To'=>('1XXX5551234'), 'From'=>('1XXX5551234')); //FROM must be a Twilio-purchased # | |
$followed_hashtag = 'blahblah'; //will follow #blahblah hashtag in the users tweets | |
foreach($users as $user) | |
{ | |
$url = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=" . $user . '&include_entities=true&trim_user=true'; | |
$user_stream = json_decode(file_get_contents($url), true); | |
foreach($user_stream as $tweet) | |
{ | |
$rows = mysql_num_rows(mysql_query('SELECT * FROM tweet_to_sms_archive WHERE tweet_id="'.$tweet['id_str'].'"' )); | |
if(!$rows && $tweet['entities']['hashtags']) | |
{ | |
foreach($tweet['entities']['hashtags'] as $hashtag_array) | |
{ | |
if(strtolower($hashtag_array['text'])==$followed_hashtag) | |
{ | |
$this_sms = $post_array; | |
echo $tweet['text']; | |
$this_sms['Body'] = utf8_encode('' . $user . ': ' . $tweet['text']); | |
if(send_sms($this_sms)) | |
{ | |
$sql = 'INSERT INTO tweet_to_sms_archive (tweet_id,body) VALUES ('. $tweet['id_str'] . ',"' .mysql_real_escape_string(json_encode($tweet)) .'")'; | |
} | |
} | |
} | |
} | |
} | |
} | |
function send_sms($post) | |
{ | |
$encoded = ""; | |
$user_name = ''; //from twilio | |
$pw = ''; //from twilio | |
foreach($post AS $key=>$value) | |
$encoded .= "$key=".urlencode($value)."&"; | |
$encoded = substr($encoded, 0, -1); | |
$twilio_url = "https://api.twilio.com/2010-04-01/Accounts/$user_name/SMS/Messages"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $twilio_url); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch,CURLOPT_USERPWD,"$user_name:$pw"); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded;charset=UTF-8")); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
return $result; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment