Posts Tagged ‘PHP’

Build your own URL shortener

Monday, August 30th, 2010
There are a lot of URL shorteners out there such as bit.ly, tinyurl.com, is.gd, tiny.cc. The list of services is large. For those of you that don’t know what a URL shortener is, it’s a piece of software that converts very long URLs such as:

http://www.blyon.com/blog/index.php/2010/08/28/build-your-own-url-shortener

Into: http://blyon.com/?efa

This make the URL easy to cut and paste and reduces the characters you use for things such as Twitter.

I’ve been using TinyURL.com for ages. This morning I wondered to myself, “why not just use blyon.com?” After all, the domain is shorter than TinyURL by 3 characters and if it’s just me using my service the hash only needs to be about 3 characters long because I am not storing millions of URLs. Other services use a long hash (7+ characters) because they need more unique ways to identify URLs.

It took all of 30 minutes to write the code, and maybe another 30 to debug it.

Here’s how it works and how to setup your own!

Go to http://www.blyon.com/t to input the URL and get the short URL which uses http://blyon.com/? as the base. There’s a simple PHP script inside /t/ which creates the hash and puts it into a two table MySQL database. I used /t/ just because I already had something at the root of blyon.com.

I added another snippet of code on blyon.com’s index.php page which figures out if someone is trying to lookup a hash or not. If it finds a hash that’s valid, it does a 302 redirect to the right location. The 302 redirect is an easy location director built right into HTTP, so basically you’re telling a browser that goes to the short URL to go somewhere else.

Now, why do this? For starters I was trying to wake up and wanted to have something to do while drinking coffee (the NPR stream was down). However, there may be a lot of SEO reasons to do this. For one, all my links I post are linked back to blyon.com, which means if something I tweet goes around the block a few times, it will all point back at blyon.com. Taking this one step further might mean that I should also allow for special keywords to be created, thus helping google learn indexicals that I want valued for my web site. The third great reason to do this (besides keeping oneself busy while drinking coffee) is to maintain your own links. If TinyURL goes down someday, everything you have ever linked to will stop working as well. However, who’s to say your (or my) admin skills are better than theirs.

Anyway, here’s the code!

The entire package as a tar: redir.tar.gz

To create the TinyURL you need this PHP code (my /t/ index page): index.php

The database include code: dbconfig.php

To figure out if a request is for your main index page or a shortened URL: mainindex.php

The MySQL database you’ll need: redir.mysql

PS: Thanks to Nathan Hickson for helping me debug this.

Export your Tweets with PHP

Tuesday, June 30th, 2009

Exporting your Tweets via PHP should be simple. There are several tools that exist but they are annoying and overly complicated.  If you want to print your last 20 Tweets on your web page (like I did with the front page of blyon.com), you can now use my simple script.

The first hack at it failed quickly because Twitter actually rate limits how many times a single host can pull the XML data per-hour.  To fix the rate limit issue I added a very basic cache function to the script which limits the requests to 60 an hour.

Setup and usage is simple, you include the PHP as you always would:

include("./twitter-1.0.php");

Configure the script by editing these items:

/* define where to store the cached result */
$CACHEDIR="/tmp/";

/* define your username */
$USERNAME="BarrettLyon";

/* define the max number of tweets to output */
$MAXTWEETS=20;

/* error message when twitter fails */
$ERRORMSG="Awh... Twitter is broken/down/not working/unhappy...";


Then call twitterPrint() or just hack the script up to do what you want.

If you make changes or have improvement ideas, just comment on this posting and I will update the code.

Get the code here: twitter-1.1.php.

UPDATE: I fixed a bunch of bugs, as a result I released v1.1.

Barrett Lyon creates fun companies that do all sorts of innovative exciting things with video and security.

CDN cdn
BitGravityBitGravity Barrett Lyon
BitGravityBitGravity
Barrett Lyon
LimeLight Networks LimeLight Networks
EdgeCast EdgeCast
CDNetworks CDNetworks
Consulting Consulting
Speaker Speaking Opportunity
Speaker Speaking Opportunity
Content Delivery Network Content Delivery Network
Content Delivery Content Delivery
Flash Streaming Flash Streaming
Interactive Video Interactive Video
Live Streaming Live Streaming
Live Video Live Video
Streaming Audio Streaming Audio
Streaming Media Streaming Media
Video Delivery Video Delivery
Video Hosting Service Video Hosting
Video Podcasting Video Podcasting
Video Podcasts Video Podcasts
Video Services Video Services
Video Streaming Video Streaming
Barrett Lyon Barrett Lyon
Barrett Lyon Barrett Lyon
Barrett Lyon Barrett Lyon
Barrett Lyon Barrett Lyon
Barrett Lyon Barrett Lyon
Barrett Lyon Barrett Lyon
Barrett Lyon Barrett Lyon