Home > Web > How to tweet from your website with your own tinyurl URL shortening service

How to tweet from your website with your own tinyurl URL shortening service

October 30th, 2009 Ed Leave a comment Go to comments

As this is my first post about Magic Hat, a little introduction. Magic Hat is a site for magicians to learn magic, with over 50 thousand users. I started it in 2004, and since then have gradually (or not so gradually) being changing it. We’re now at the 8th major redesign. It started as a good way to learn PHP/MySQL/HTML, so I didn’t touch any content management systems, and wrote the entire first 3 versions with linux’s best text editor, vim! I then treated myself to using Eclipse, which saves a lot of work – Ctrl + Space and your website’s done (I wish!). Then I chucked in a phpBB forum, modified it and my site so that they’re nicely integrated. Finally, I changed to using smarty templates, which has made changing the look of the site in the last few versions significantly easier – would highly recommend it.

So, you’ve probably gathered that I’ll happily reinvent the wheel many times over, but hopefully for a good result. Magic Hat is very dynamic and has lots of complicated features that I don’t think would have been feasible to get working in something like WordPress without having to learn and change most of WP.

I’ve setup the Magic Hat forums to tweet every time someone posts, but I didn’t really want to use tinyurl/bit.ly as I’d like to keep magichat.co.uk in the links.  So, here’s some code that creates the smallest possible hash.

$letters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
function getHash($i) {
        $string = "";
        while ($i >= strlen($letters)) {
                $string = substr($letters, $i % strlen($letters), 1) . $string;
                $i = floor($i / strlen($letters));
        }
        $string = substr($letters, $i, 1) . $string;
        return $string;
}
function getNumFromHash($string) {
        $letters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $k = 0;
        $count = 0;
        for ($j = strlen($string) - 1; $j >= 0; $j--) {
                $letter = substr($string, $j, 1);
                $indexOfLetter = strpos($letters, $letter);
                $k += $indexOfLetter * pow(strlen($letters), $count);
                $count++;
        }
        return $k;
}

So, whenever I want to twitter something, I stick an entry in a database table with the URL and text contents. I grab the auto incrementing ID from the primary key, and pass it into getHash(). Then I run this:

$twitters = sql_array("SELECT id, message, link FROM twitter WHERE sent = 0 ORDER BY id");
if (count($twitters[0]) > 0) {
        foreach ($twitters as $t) {
                $linkLength = 0;
                $link = "";
                $message = $t['message'];
                if (strlen($t['link']) > 0) {
                        $link = "http://magichat.co.uk/w/".function_getHash($t['id']);
                        $linkLength = strlen($link) + 1;
                        $message = substr($message, 0, 140 - $linkLength);
                        $message .= " ".$link;
                }
                $message = urlencode($message);
                $ch = curl_init('http://twitter.com/statuses/update.json');
                curl_setopt ($ch, CURLOPT_POST, true);
                curl_setopt ($ch, CURLOPT_POSTFIELDS, 'status='.$message);
                curl_setopt($ch, CURLOPT_USERPWD, 'magichatuk:PASSWORD');
                curl_setopt($ch, CURLOPT_VERBOSE, 0);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
                $response = curl_exec($ch);
                curl_close($ch);
                sql_update("twitter", "sent = 1", "id = ".$t['id']);
        }
}

sql_array and sql_update are a couple of functions that I’ve written to do stuff with the database, and cache results etc.

The code ensures the full URL is present by shrinking the text if need be.

This line in my .htaccess file directs everything starting /w/ to tinyurl.php

RewriteRule ^w/(.+)    /tinyurl.php?hash=$1

tinyurl.php:


$string = $_GET['hash'];
$k = getNumFromHash($string);
$url = sql_result("link", "ma_twitter", "id = $k");
sql_update("ma_twitter", "hits = hits + 1", "id = $k");
header("Location:http://www.magichat.co.uk".$url);

And the job’s done! Doing it this way (without using tinyurl) means that you can jump to #anchors on the page.

See it in action at http://twitter.com/magichatuk

Categories: Web Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.