PHP script to automate tweeting

[UPDATE – as of August 31st 2010, Twitter has removed basic auth as a valid access method to the Twitter API. As such, the method shown below will no longer work. I will be posting a variation to it soon, which uses oAuth as required by Twitter’s terms.]

As part of my fun rummagings and the putting together of one of my upcoming sites, I wanted to add the functionality of being able to automate posting to Twitter. I had already sorted providing the user with the option of bookmarking or posting their page to whatever social site they wished (here’s my post about that), however I thought it might be fun to have a twitter feed updating with the new pages as they were generated and altered.

It’s a pretty straight-forward bit of code, and you can just copy-paste it into your code and use it immediately (you will need to fill in your twitter account details, customise the tweet, and ensure you have the php_curl extension activated).

Here’s the code:

$username = 'myUserName';
$password = 'myPassword';
$status = 'This is a new Tweet!';

if ($status) {
$tweetUrl = '';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "$tweetUrl");
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "status=$status");
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");

$result = curl_exec($curl);
$resultArray = curl_getinfo($curl);

if ($resultArray['http_code'] == 200)
echo ‘Tweet Posted’;
echo ‘Could not post Tweet to Twitter right now. Try again later.’;



As I said, it’s pretty straight-forward, and does the job. You can add in more functionality pretty easily – this is purely a base-case scenario. I would assume that you would want to make the tweet dynamic (I did) and do some error checking. If you are going to use this script as a user input handler, you will want to ensure that all their inputs are cleaned and valid.

Change myUserName and myPassword to your login details for Twitter. When the script is pointed at by your browser, it will make a tweet automatically. You can add additional result codes as required to distinguish scenarios; the Twitter API is here.

[Edit: I have added another tutorial, which follows on from this one. It is a nice simple addition to this one which handles each different response code from the Twitter API uniquely.]

[sourced via BrownPHP]


About Cameron
I'm a final year Computer Science/Information Systems major. Already finished my BA in Politics/Philosophy. I do web and software freelance on the side, while I finish studying. Hoping to be self-employed by the end of my degree, otherwise off into the real-world I go....

2 Responses to PHP script to automate tweeting

  1. Pingback: Twitted by camerontwigden

  2. Pingback: handling Twitter API response codes (cURL, PHP) « whileloop

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: