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.

today is a day is a day

Well, the site is definitely done now (except the final cronjob stuff – yep, it’s still hanging around!!) and I worked some hacks and fixes for dealing with Internet Explorer….ran trials in my 9 or 10 browsers, and the only fails were IE, Maxthon, and other IE-based browswers. Solved that by scripting two sets of css and JavaScript dependant on what the browser in use is. Why can’t Microsoft just build a real standards-compliant browser and be done with it?? I’m probably going to delay actually going live for another few weeks as I have two more exams to prioritise, then I fly out the following morning for a break, so realistically won’t get to confirm my testing on the live site ’til after I get back.

This morning I had the privilege of watching a pod of orcas swimming up along the coastline of the Waitemata Harbour, and back down again. They’d been inshore over-night, and then started moving around again. When I first saw them, they were surfacing quite a bit and display a lot of body and dorsal fin, as well as some white patches. By the time I raced home and got my camera, they’d calmed down a bit and were only surfacing enough to show a small amount of dorsal. Beautiful creatures, but unfortunately my pictures don’t show this to great effect. I’ll post one pic, just so you can see a bit, and also the beauty of the islands out in the harbour.

Orcas in the harbour - 24 June 2009

Otherwise, just been studying hard (trying to!) and getting ready for exams. Haven’t done much prep or planning for trip yet, but shouldn’t need much. I’ll be packing on Monday night after the exam, because I figure I don’t need sleep and can do that on plane. I’ll also be re-loading my laptop that night. I’ve got to install Office Pro once I can take my time…I still have to use MS because of specified programs for my Information Systems papers (MS Project is a required submission standard), but my other box is about to be re-built as a Linux and LAMP box. Again, just haven’t had the time to do it so far this year, and allowing time to remedy issues. It will happen, just all in good time.

In other news, what I predicted all along has come to be true for thestory about the Belgian girl who got 56 stars tattooed on her face. She has now admitted to lying because her father was angry (link here @ dailymail.co.uk). I knew no tattoist would “accidentally” or deliberately do 56 stars when only asked for 3. At least he has now been vindicated after a week of crap from everybody.

Best go back to the study, so I’ll just leave you with this great version of Katy Perry’s “Hot and Cold”:

Enjoy.

it’s been a while

Well, it’s taken me a while to get to posting….exams have been interesting, two down, two to go. Been doing a LOT of work on one of my sites, and it’s all good to go. Just sorting out domain registration and hosting today and tomorrow; then spend the next few days hammering the site to make sure it doesn’t break. Then announce it to the world!! It’s been a bit of a journey, but this one has come form scratch in little under a month; which I don’t think is too bad given I conceptualised, planned and implemented it while still knocking out assignments and tests, as well as these exams. Oh, yeah, two assignments 100%, one assignment 99.5%, one assignment 90%. The 90%-er had a class average of 57% so I’m happy there, however the 99.5%-er I’m a little miffed about, but they haven’t given marking schedule back yet and we can’t ask for it at present because of “no contact” rules due to exams.

I’m looking forward to exams all being over though, as by then I’ll have the site launched and doing its thing, so I’ll have to relax in Brisbane. I’ll be typing up some tutorials here and there while I’m away, so I can post them all up rapidly when I get back. Once I’m back, the other site will get some focus, and hopefully come together pretty quickly with all I’ve taught myself on the current one. I’ve got some great JavaScript stuff pulled together for it now, so I’ll be highlighting some MooTools and other script sources (LightBox2 is also great!!). I’ve been finding some great inspirations online, and pulling some things into composite in (what I think is) some intriguing ways. Chances are I’m just reinventing the wheel, but I like it anyway.

I need to highlight this for everybody. I got it through Time on twitter, but it is serious. I know there’s a lot of people getting damaged in Tehran and Iran at present, and I don’t want to demean the lives of the rest of them, however this does bring it that bit closer to home for all of us. One of LIFE magazine‘s Iranian photojournalists has “disappeared” (link is here) while reporting.

Here’s a great vid to tide you over too (found through the guys at dailyfailblog.com):

I have not heard anyting further from Google Adsense regarding my disabled account (original blog post here), so I am just going to see what happens by Wednesday this week. Once I launch the site, I will register with one of the myriad of AdSense alternatives and get some ads pumping that way. I’m not expecting to retire off the ad revenue from this, but it would be nice to fund the hosting and domain registration so that it doesn’t cost me. I’m happy either way with costs though, because I don’t want to charge for the use of the site, even though it is a service (and I’ve endeavoured to make it a worthwhile, and exceed expectations – a lot of the recent work has been to make the GUI side of it pleasing, and not just functional), because I firmly believe the internet should be free as a principle.

I’ll talk about the site some more once it launches, because there’s a lot of credit needs to be given to various other sites for inspiration and resources (especially for the GUI side of it all). Social iconsets are a dime-a-dozen out there, however finding a decent set ahich is actually useful and is up to date in terms of content is not so easy. A lot of sets have most social network/bookmark site logos minus one or two, but they always seem to be missing one of the more important ones (I found one set that didn’t do facebook!!). I’ve been working a lot more in gimp and PhotoShop too – my skills are way down the scale in terms of what is achievable, but it’s better than nothing, and I’m expecting to be well upskilled over the few weeks after exams – so that I could get the graphics on the site being real graphics, and not just HTML/css tricks with background/opacity etc.

Anyway, back to the study now, but I’ll post a bit more consistently this week, and hopefully reasonably well in the following week while I’m overseas.

Ciao.

quick update – holiday tickets booked!!!

YAY!!! Just scored my tickets with Naomi to Brisbane and Gold Coast wicked cheap!! All booke dand paid. YAY!!

Scored awesome deal from AKL to BNE for NZ$165, and from OOL (Gold Coast) to AKL for NZ$250 including taxes!! Wicked!! AirNZ and Qantas, and all the online deals, tried to scam us over $500 return etc. WebJet even just sent me their BEST deal, and it was more than I just paid….5 minutes apart from when I clicked pay, to WebJet’s email.

FTW!!! Just had to share.