Gabe
Gabriel Richards
Founder & CEO
Blog

Quick Tweet with PHP - How to integrate Twitter with 20 lines of code

I found this tidbit on php.net. Super quick and easy tweet from within PHP courtesy of Fabien Potencier.

<?php  
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:

<?php  
tweet('From PHP, yeah...', 'fabpot', 'Pa$$');  
?>

Our Latest Tweets