How to make your own Twitter Poster in PHP with Curl
Wednesday, December 23rd, 2009Twitter 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.
Popularity: 6% [?]


admin says:
December 30th, 2009
3:22 pm
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.
Leon Horton says:
March 15th, 2010
6:25 pm
Ken Reno put together a couple of WordPress plugins autopost to your WP blog and then will automatically post to your Twitter account. It allows you to set the frequency of updating your blog therefore the number of tweets is regulated by the updates. Look it over here… http://www.commissionsautomator.com/?e=leonh
For such a cheap price, I think it would be the thing to do. Plus it is a whitehat way to do it in my opinion.
Peace,
Leon