Tweeting from PHP
Fabien Potencier
June 14, 2009
Twitter is everywhere nowadays. Odds are eventually you will want to tweet
from PHP. No need to use one of the numerous PHP Twitter libraries, as
tweeting is as simple as using the PHP built-in file_get_contents()
function:
function tweet($message, $username, $password) { $context = stream_context_create(array( 'http' => array( 'method' => 'POST', 'header' => sprintf("Authorization: Basic %s\r\n", base64_encode($username.':'.$password)). "Content-type: application/x-www-form-urlencoded\r\n", 'content' => http_build_query(array('status' => $message)), 'timeout' => 5, ), )); $ret = file_get_contents('http://twitter.com/statuses/update.xml', false, $context); return false !== $ret; }
Pretty easy, no? Using the tweet() function is of course a piece of cake:
tweet('From PHP, yeah...', 'fabpot', 'Pa$$');
As an added bonus, the function returns true if everything went fine, or
false otherwise.
As we talk about Twitter, you can follow me.




Discussion
Changing the above code to a GET request, and removing the "Content-type/content" sections and calling the URL http://twitter.com/statuses/user_timeline.json will get you back your JSON timeline...
function postTweet($message, $username, $password)
{
$url = 'http://twitter.com/statuses/update.json';
$fld = http_build_query(array('status' => $message));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fld);
$ret = curl_exec($ch);
return false !== $ret;
}
<?php
$MsgToTweet = "Dude, welcome to programming. :) " .
"$_SERVER['SERVER_NAME']" .
"$_SERVER['PHP_SELF']" ;
$TwitterUserName = "Rob";
$TwitterPassword = "Rob'sPassword";
tweet ($MsgToTweet,
$TwitterUserName,
$TwitterPassword);
// Cut and paste Fabien's code defining the tweet function here.
?>
p.s.
This page of predefined server variables in PHP might be helpful to you (depending on what you're trying to do).
http://us2.php.net/manual/en/reserved.variables.server.php
Identi.ca is being used in a lot of places, as it contains many features which exceed that which you currently get from Twitter! Including; XMPP, Groups, Messaging to tagged followers and, best yet, it's being developed in the open.
If you're interested, check out http://laconi.ca for the source code, and http://identi.ca to see how it works!
return false !== $ret;
and not
return (bool) $ret;
Both constructs are different: false !== $ret returns true only if $ret is exactly equal to false. Your construct also returns false if $ret is the empty string for instance, which can be a valid return value.