Let PHP Randomly Update Your Twitter Feed

So you’ve got a Twitter account…and you find yourself in one of the following situations:

  1. You’re going on vacation, but don’t want to have people thinking you’re MIA.

  2. You’re sick and tired of updating your twitter account and you don’t want people “nudging” you.

  3. You have an NMS — a “new media stalker.” So you’ve created a secondary twitter account for them to follow but don’t want to actually update it yourself.


//
//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
?>

Now you have a couple of choices. Place this on your web account and run a cron every few hours. Or, run it locally. Throw it in your bookmarks list and call the PHP page a few times a day. It all depends on how dedicated you are to your little ploy. Enjoy!

Hi there, I'm Jon.

Writer. Musician. Adventurer. Nerd.

Purveyor of GIFs and dad jokes.