Tiny url encode and decode with PHP

The first functions make short urls using tinyurl.com service. It’s a very popular function on the web, you can find…

December 29, 2009

The first functions make short urls using tinyurl.com service. It’s a very popular function on the web, you can find it in a lot of files, and it’s always the same:

function doShortURL($url) {
	$short_url= file_get_contents('http://tinyurl.com/api-create.php?url=' . urlencode( $url ) );
	return $short_url;
}

The second function decode a short url in a simple way: I’ve used CURL to get the http header of the short url page. If the header contains a “Location:” header, then it’s a redirect, and the decoded url is the url in the “Location” header string, here is the code:

function doShortURLDecode($url) {
	$ch = @curl_init($url);
	@curl_setopt($ch, CURLOPT_HEADER, TRUE);
	@curl_setopt($ch, CURLOPT_NOBODY, TRUE);
	@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
	@curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	$response = @curl_exec($ch);
	preg_match('/Location: (.*)\n/', $response, $a);
	if (!isset($a[1])) return $url;
	return $a[1];
}

These functions are included in the Mini Bots Class.

Author

I’m a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I’m the founder of Rockit.it and Dailybest.it and I’m actually the Chief Technical Officer of Better Days web agency.


Recommended

Bot that retrieves url meta data and other infos

From a given url this function retrieves page title, meta description, keywords, favicon, and an array of 5 images to…

January 12, 2010

PHP bot to grab meteo information from Google

Google has many usefull functions that give you data fast, such as cinema infos, or for meteo forecasts. I think…

December 24, 2009

Orari trenord, corri solo quando ce n’รจ bisogno

Hai presente quando corri in stazione per prendere il treno al volo e, quando arrivi, ti accorgi che il treno ha 12 minuti di ritardo?

March 30, 2017

Remove archive pages in WordPress, how to

I think that in most blogs (that is not all) archive pages are redundant content and there’s no need to…

September 8, 2015

How many times a web link has been shared on Twitter

Twitter share button and Facebook share button are the most used buttons to share links on Internet. You can read…

October 19, 2012

get MySpace events with a PHP function

Here is a function to read the concerts for a myspace band page. This code retrieves the “shows page” for…

February 21, 2011