//
//Twitter updater, v1.0
//by Jon McCartie
//
//functions first
function twitter_post($message) {$username = ‘yourtwitterusername’;
$password = ‘yourtwitterpassword’;// The twitter API address
$url = ‘https://twitter.com/statuses/update.xml’;
// Alternative JSON version
// $url = ‘https://twitter.com/statuses/update.json’;
// Set up and execute the curl process
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, “$url”);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, “status=$message”);
curl_setopt($curl_handle, CURLOPT_USERPWD, “$username:$password”);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);// check for success or failure
if (empty($buffer)) {
$msg = “ERROR connecting to Twitter”;
return $msg;
} else {
$msg = “SUCCESS!! TWEET SENT!”;
return $msg;
}
}//begin loop. this loop is needed to make sure the quote we get is less than 140 characters
$i=0;
while ($i == 0) {//get random quotes XML — we’re using quotedb.com because they return quotes in XML
$url = “https://www.quotedb.com/quote/quote.php?action=random_quote_rss&=&=&”;//this CURL script allows for the URL to be passed without exposing PHP to a allow_url security bug
$ch = curl_init($url);
$fp = fopen(“temp2.xml”, “w”);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);//Load the local XML that was created in the above CURL call into an array
$xml = simplexml_load_file(“temp2.xml”)
or Die (‘ERROR: Could not load file’);//read the first item’s quote, and strip the quotes
$message = trim($xml->channel->item->description,”\x22\x27”);//if the message is less than 140 characters,
if (strlen($message) <= 140) {//fire the Tweet
$msg = twitter_post($message);
echo “Success!”;
$i = 1; // set $i to 1 so we can end the loop
} //end if
} // end while loop
?>
Writer. Musician. Adventurer. Nerd.
Purveyor of GIFs and dad jokes.