By brandon.corbin on Sunday February 7th, 2010

  Post to Twitter PHP Function (915 bytes, 93 hits)

Looking to Tweet from PHP? This little function will do exactly that. Simply supply a username, password and Tweet, and thanks to CURLs black magic your message will be Tweeted. You can also specify which format you would like the response in – either JSON or XML, with JSON being the default.

<? function tweet($username, $pword, $status, $format='json') {
$ch = curl_init(); // init curl
curl_setopt($ch, CURLOPT_URL, "http://twitter.com/statuses/update.".$format);
curl_setopt($ch, CURLOPT_POSTFIELDS, "status=$status");
curl_setopt($ch, CURLOPT_USERPWD, "$username:$pword");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$bf = curl_exec($ch); // execute curl call
curl_close($ch); // close curl handler
// Return true or false demending on results
return $bf;
} ?>

Using the PHP to Twitter function
echo tweet('TwitterUSERNAME','Password','Rocking the PHP-To-Twitter function by @brandoncorbin');

  Post to Twitter PHP Function (915 bytes, 93 hits)

8 Responses to “Post to Twitter from PHP”

  1. wbcnews says:

    Post to Twitter from PHP with this super simple function – http://bit.ly/bCUu2n

  2. brandoncorbin says:

    Post to Twitter from PHP with this super simple function – http://bit.ly/bCUu2n

  3. yarkyang says:

    @bang590 底下还有一个错误:
    Fatal error: Call to undefined function curl_init() in www/t/lib/twitter.php on line 775

  4. wyfusky says:

    @Showfom 你好,我在使用twiteseV1.3的时候提示lib/lib/twitter.php on line 832 Fatal error: Call to undefined function: json_decode() in 有有什么解决之法吗

  5. butwait says:

    Phooey. http://educon22.org = “Fatal error: Call to undefined function apc_fetch() in /var/mics/code/Plugins/functions/twitter.php line 9″ ?

  6. adasd says:

    fuck you all

  7. This will fail from mid-August, when Twitter mandates oAuth.

Leave a Reply

← By brandon.corbin

I am Brandon Corbin, a web entrepreneur with a background in advertising and design. For over a decade I have applied my passion for user experience, marketing and software design to industries like: radio, real estate, pharmaceuticals, recruiting and eCommerce.