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:

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

if ($status) {
$tweetUrl = 'http://www.twitter.com/statuses/update.xml';

$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’;
else
echo ‘Could not post Tweet to Twitter right now. Try again later.’;

 

curl_close($curl);
}
?>

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]

exams over, off for a break

ZOMG!!! Exams are finally over. The last one was better than expected. It was InfoSys, which is one of those subjects where you now how to do everything, but they still manage to find some obscure point and drill into it so that you walk out of the exam feeling like you knew nothing. Luckily, that was not the case with today’s exam. It was all in-depth enough that you couldn’t do it without studying, however it was straight-forward and pleasantly lacking in “surprise” or “trick” questions. The only one that had me worried was the one that asked “What is the address of the business network?” having just had a preamble which included “You have been given the address space 192.38.45.128/25” (or numbers to that effect). The question actually threw me as I tried to find an alternate answer, but yes, I checked with others afterwards and it was that easy…..just copy the address space given in the preamble into the answer space. Yippee!!!

I have to say it is a nice feeling to walk out of an exam knowing you did well, and your study was correctly focussed and worthwhile. Definitely a great way to end the exam season.

So, now I’m on holiday!! Off to Australia in a few hours time, for some much needed R’n’R. And a long looked-forward to catch up with Michelle and Kristian. I haven’t seen them since their honeymoon started, pre moving to Aussie. Also going to meet up with some friends of Naomi’s and stay with them for a bit too. Hopefully Kristian managed to score some tickets to the Broncos’ home game this Friday as they are playing the Warriors! Be great to go to that and support a kiwi team on kangaroo land.

I’ll keep posting here when I can while out and about over there, but my twitter feed will be the most up to date as I’ll text to it regularly. Plans have been made for Movie World, as well as the afore mentioned Warriors/Broncos game, and a possible wine tour on the weekend. I can’t wait to get back to South Bank, as I haven’t made it there for almost 5 years (Oct/Nov 2004 to be exact-ish).

I’m going to do up some tutorials and sample scripts on the plane, so I will hopefully have them posted within a day or two to add some bulk to the blog. I was going to add them as pages, but the formatting and indexing seemed to go meh, so I’ve just left them as blog posts. That said, I’m going to create a single page which indexes all the blog posts of a tutorial-ish nature for easy reference and location, otherwise the tags alone will just snowball in the end.

Best get back to the packing, and I’ll try to find some cool stuff for pretty-ness for the next post.

social bookmark generation

I thought it could be useful to post some basic social bookmarking scripting. This is nothing flash, and you can do a lot more with the various APIs available for each site, however this gives a good basic starting point.

The code I have put here will simply open a new window/tab (depending on user browser settings) which will open at the specific social networking/bookmarking site with pre-formatted and written information as appropriate.

<?
$actual = [INSERT YOUR PAGE URL HERE];

// del.icio.us it
echo '<a href="http://del.icio.us/post?url=';
echo $actual;
echo '&title=';
echo $actual;
echo '" target="_blank">del.icio.us</a> ';

// digg it
echo '<a href="http://digg.com/submit?url=';
echo $actual;
echo '&title=';
echo $actual;
echo '" target="_blank">digg</a> ';

// facebook it
echo '<a href="http://www.facebook.com/sharer.php?u=';
echo $actual;
echo '" target="_blank">facebook</a> ';

// reddit
echo '<a href="http://reddit.com/submit?url=';
echo $actual;
echo '&title=';
echo urlencode($actual);
echo '" target="_blank">reddit</a> ';

// stumbleupon it
echo '<a href="http://www.stumbleupon.com/submit?url=';
echo $actual;
echo '&title=';
echo urlencode($actual);
echo '" target="_blank">stumbleupon</a> ';

// tweet it
echo '<a href="http://twitter.com/home?status=Check out ';
echo $actual;
echo ' " target="_blank">twitter</a> ';

// email it
echo '<a href="mailto:?subject=Check out ';
echo $actual;
echo '&body=Check out ';
echo $actual;
echo '.">email</a> ';

// blinklist it
echo '<a href="http://www.blinklist.com/index.php?Action=Blink/addblink.php&Description=';
echo urlencode($actual);
echo '&Url=';
echo urlencode($actual);
echo '" target="_blank">blinklist</a>';

// bluedot it
echo '<a href="http://bluedot.us/Authoring.aspx?u=';
echo $actual;
echo '&t=';
echo urlencode($actual);
echo '" target="_blank">bluedot</a>';

// magnolia it
echo '<a href="http://ma.gnolia.com/bookmarklet/add?url=';
echo $actual;
echo '&title=';
echo $actual;
echo '" target="_blank">magnolia</a>';

// netvouz it
echo '<a href="http://netvouz.com/action/submitBookmark?url=';
echo $actual;
echo '&title=';
echo urlencode($actual);
echo '" target="_blank">netvouz</a>';

// blogmarks it
echo '<a href="http://blogmarks.net/my/new.php?mini=1&simple=1&url=';
echo $actual;
echo '&title=';
echo urlencode($actual);
echo '" target="_blank">blogmarks</a>';

// simpy it
echo '<a href="http://www.simpy.com/simpy/LinkAdd.do?href=';
echo $actual;
echo '" target="_blank">simpy</a>';

// diigo it
echo '<a href="http://www.diigo.com/post?url=';
echo $actual;
echo '&title=';
echo urlencode($actual);
echo '" target="_blank">diigo</a>';

// yahoo it
echo '<a href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=';
echo $actual;
echo '&t=';
echo urlencode($actual);
echo '" target="_blank">yahoo!</a>';
?>

I have used a default variable $actual for this version in PHP, however you can easily alter this to contain just hard-coded HTML (I don’t know why you would exactly, as then you will have to code each page individually), also I have generated all the HTML within the PHP script as I have taken this from a larger script with more dynamic variables being referred to. Simplicity, if this is all you are wanting to do, would be to strip the
<?php....?> surrounds and all the echoes; replacing each $actual with <?php echo $actual; ?>.

To replace the links with icons, you can try some of the following sites to get social network/bookmarking icons.

Please read the terms of use for each set of icons, and comply with licensing requirements.

If you are using icons, you can create a simple hover effect by wrapping the links as so:

<div id="bookmarks">
...
// insert bookmark links here
...
</div>

inserting the following into your CSS file:

#bookmark a img {
opacity: 0.8;
alpha.opacity: 80;
}

and then adding the following to each img in your bookmarks links:

onmouseover="this.style.opacity=1; this.filters.alpha.opacity=100" onmouseout="this.style.opacity=0.8; this.filters.alpha.opacity=80"

For example, the following can be pasted into a blank document, and saved as an HTML document:

<html>
<head>
<style type="text/css" >
#bookmarks a img {
opacity: 0.8;
alpha.opacity: 80;
}
</style>
</head>
<body>
<div id="bookmarks">
<a href="https://whileloop.wordpress.com/2009/06/28/social-bookmark-generation/">
<img src="http://i572.photobucket.com/albums/ss166/ctwi001/blogging/whileloop/twitter.png" title="twitter" alt="tweet me" onmouseover="this.style.opacity=1; this.filters.alpha.opacity=100" onmouseout="this.style.opacity=0.8; this.filters.alpha.opacity=80" />
</a>
</div>
</body>
</html>

Copy this code to a new TextPad or NotePad document, save it as HTML, and then open it in your browser. This provides a twitter icon which highlights on mouse over. The link when it is clicked on brings you to this page.

I will respond to queries in the comments, so ask away if clarification is required (I hope I’ve been clear, yet succinct) or you want assistance with further extensions to what I have started here.

quick update – iPhone 3G[S] is waterproof??

Had to quickly post to share this video. A muppet with his brand new iPhone 3G[S] is showing off the new video recording functionality when he drops it in a swimming pool! The best part is the iPhone not only doesn’t die, but actually keeps on recording the whole incident….

Not the cleverest of tricks to pull off, but sure to be a crowd pleaser none-the-less.

Oh, and as an update to my post about trying to score some money back from the IRD (here), I did call them today. The result being that they decided that they had made an error in their most recent letter, and they now accepted that I had earnt money last year. Funny that, I told them I was happy to accept their most recent decision, however they preferred to refund me a couple of hundred dollars, rather than a few thousand dollars. Apparently it will now take at least 6 weeks to process?? Hmmm, I said, you’ve just done the whole process on the phone for me right now; what more is there to do except start the direct credit to my account? Ah, well, that’s the government for you; always tking extra long to give you money back without interest, but penalising you immediately if they think you might owe them money.

Anyway, back to the study now.

today was

Today was a sad day for much of the world with the passing of MJ, The King of Pop, at the age of 50 of a heart attack. Also passing away, but with much less fanfare due to Michael’s passing, was Farrah Fawcett of Charlie’s Angels (the original good one) fame. She too was an icon of the entertainment industry, and will be missed.

Meanwhile, I had another exam, which was notable for it’s content as always. The first question being worth 53 of the 120 marks was awesome in so many ways!! Anyway, it’s done now, and on to the next one. Having said that, this paper has been great because it pushed me into different ways of doing my database set up etc, which has been great as I write the sites. It also made me get in-depth on my xml and styling of xml, so all-in-all not a bad thing.

I have one more exam to go, on Monday afternoon, then it’s off for a week of relaxation and no real scripting or coding. Dependent upon internet access I’ll be blogging still with photos, so I should get back into at least daily updates. Check my twitter though, because I’ll be updating that more regularly from my phone and so on. On that note, please come follow me on my twitter so I can feel the love. I broke 30 followers the other day!!! So I’m not in Ashton’s league yet. Packing for the Brisbane trip will be Monday evening and through the night, before leaving the house at 3 or 4 in the morning.

I’ve planned my flight already, as I am intending to have backed up all my data before flight so I can wipe my laptop and do a whole new clean start. I’ve got my Windows 7 ready (or maybe an XP Pro install), as well as all my Office Pro and other software ready. I have to still use MS Windows because ONE of my papers requires assignment submissions in MS Project. No Ubuntu for me then, except the rebuild of the server box once I get back from Aussie. I’m also hoping to get a bit of work done on my tutorial type pages, so I can add them to here once I get back from Aussie. All these promises I keep making, but it is true: the next few weeks will have the site launch (hopefully the 2nd not too far after) and also have the tutorials hit the web too.

I’m also gonna be having an interesting conversation with the IRD tomorrow, as they just declined my claim for donation rebates as

[t]ax credits can only be allowed for amounts paid or donated that are equal to or less than your total income. [Their] records show that [my] income from salary/wages or benefits for the year was $0.00.

That’s cool, and I’m not going to dispute that, as it is the most recent communication from them and so the most up to date. The interesting part will come when I ask them (on the basis of a letter received two weeks earlier) to refund me the money they acknowledge I paid as PAYE (Pay As You Earn income tax) as since I earned nothing (according to them), I really should have no tax to pay. I’m sure they will kick up a stink at me, and find new records; but all I can do is look at the veracity of what they officially document to me, and as they have officially told me – I earned nothing.

I’ll keep you updated on this, because I can see it turning into a bit of jargoned legalese rubbish where they find a way to say I earned nothing but owe them thousands in taxes….

Update: 27.06.09 – I did call them today. The result being that they decided that they had made an error in their most recent letter, and they now acepted that I had earnt money last year. Funny that, I told them I was happy to accept their most recent decision, however they preferred to refund me a couple of hundred dollars, rather than a few thousand dollars. Apparently it will now take at least 6 weeks to process?? Hmmm, I said, you’ve just done the whole process on the phone for me right now; what more is there to do except start the direct credit to my account? Ah, well, that’s the government for you; always taking extra long to give you money back without interest, but penalising you immediately if they think you might owe them money.

Here’s a classic bit of internet lol’age, the baby panda video. You’ve either seen it ages ago (so you need to see it again) or you haven’t seen it before (so you really do need to see it), so here it is:

That’s it for tonight, and I’ll be back with more tomorrow.

Michael Jackson dies at 50

Wow! I first started hearing the reports about 40 minutes ago, and instinctively thought scam. But, then I started searching and the story came out:

At 12.26pm Thursday June 25th, 2009, LA paramedics were called to Michael’s home. When they arrived, they found him not breathing. CPR was performed at the house and on the way to UCLA Medical Centre. Latoya was seen rushing in shortly after.

Official confirmation is just starting to filter through, however his death is being confirmed by TIME online, BBC online, the NY Times, the LA Times, and many other news networks. Twitter is virtually shut down with all the traffic at present, and the tributes are only just beginning to flow for this icon of music, popular culture, and the eighties.
Michael has seen some troubled times in the last few years, however he was always an amazing artist and performer, it’s impossible not to reflect on the eternal and profound mark Michael Jackson left on music and pop culture at large.

I’m going to try to post a YouTube video of him here, but it may fail due to the internet being hammered right now. [Update – most videos are not allowing embedding] This is one of his iconic performances:
Billie Jean @ Motown 25: Yesterday, Today, Forever.

It brought tears to my eyes even listening to it again now.
RIP Michael, we will always remember and love you.

quick update

I’m meant to be studying, and I am, but I saw this and had to share. It’s from about three weeks ago.
As the video blurb says:

Aaron Shutway, an 8th grader at Brecksville-Broadview Heights Middle School nails a front handspring to a 3/4 nothing but net shot to send the P.E. class in a frenzy! I haven’t seen Lebron hit one…


Over 9000 kindsa awesome right there!!
And for all the WoW fans out there…..no offence (yep, you know you won’t like what comes next), but this is why some people think you are too into a game.

I’ll be back with more this evening.