Tuesday, September 14, 2010

PHP Customized Tweeting for Twitter.com

Here is a php script which will let tweet on twitter.com providing your username, password and the message you want to tweet! Since this is a function, you can customize the code anyway you want to.


<?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;
}

?>


Here is a simple code on using the tweeting function.


<?php

tweet('Php code tweeting!', 'username', 'password');

?>

No comments:

Post a Comment