Having a choice of 50 Wordpress plugins are great when you are using Wordpress but what do you do when you have custom made software or don’t use Wordpress?

Using this handy cURL php code, you can post to Twitter from anywhere on your website. You can include the code as is, or just stick it into a function that you can include across all your scripts that need it.

 PHP |  copy code |? 
01
02
// Set username and password
03
$username = '$username';
04
$password = '$password';
05
// The message you want to send
06
$message = $TWITTER_POST;
07
// The twitter API address
08
$url = 'http://twitter.com/statuses/update.xml';
09
// Alternative JSON version
10
// $url = 'http://twitter.com/statuses/update.json';
11
// Set up and execute the curl process
12
$curl_handle = curl_init();
13
curl_setopt($curl_handle, CURLOPT_URL, "$url");
14
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
15
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
16
curl_setopt($curl_handle, CURLOPT_POST, 1);
17
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
18
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
19
$buffer = curl_exec($curl_handle);
20
curl_close($curl_handle);
21
// check for success or failure
22
if (empty($buffer)) {
23
//    echo 'message';
24
} else {
25
//    echo 'success';
26
}
27

I commented out the messages but I left them in for debugging purposes. Just uncomment then and start testing. Here's the end result... as if we didn't know what it would look like anyway.

cURL Twitter Post


This is really just a snippet, you can expand on this and even create forms that request the username and password for distributable scripts or replies from your traffic.
Was this useful? Help share it:
  • Print this article!
  • Twitter
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Mixx
  • Fark
  • MySpace
  • Technorati
  • Reddit