| Subcribe via RSS

Join the Black Hat Digest Newsletter

Get On The Site Creation Waiting List
Have a new website delivered to you every month! Read more...

How to make your own Twitter Poster in PHP with Curl

December 23rd, 2009 Posted in Tutorials

Twitter offers a handy dandy API for posting to itself.  It’s not hard to work with (you just have to organize your data the right way).

You need to have a few variables set up prior to running this.

$message  (what you are posting to twitter — a day or so ago I provided the code to make tinyurls)

$twitter_user =  Your twitter Username
$twitter_password =  Your twitter password

If you have those three variables set you can call the code below and post to twitter with their api url and a wee bit of curl.  Here is the code to do it:

$twitter_api_url = “http://twitter.com/statuses/update.xml?status=”.urlencode(stripslashes(urldecode($message)));
$twitter_data = $message;
$ch = curl_init($twitter_api_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $twitter_data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, “{$twitter_user}:{$twitter_password}”);
$twitter_data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
//Let’s print out where we posted and what!

echo ‘Posting Message: ‘.$message.’<br> to <br> http://www.twitter.com/’.$twitname.”;
// Let’s print out an error message if things didn’t go well.
if ($httpcode != 200) {
echo “<strong>Don’t Panic!</strong> Something went wrong, and the tweet wasn’t posted.  You Failed.”;
}

That’s really it.  It will post your message to twitter.  You can store your variables in an array, or a database, or a text file.  Along with all your messages and have it go do it’s thing.

Cash in on your Competitors’ Work – SpyFu Ad History

Popularity: 4% [?]

One Response to “How to make your own Twitter Poster in PHP with Curl”

  1. admin Says:

    One thing you can do is in the error section is have it give you a notice (through mail, text file, something). That way if your twitter account gets a problem like being banned or whatever you can stop trying to post to a dead account.


Leave a Reply