Shorten URL using Adfly API in PHP



In previous tutorials I have explained shorten URL API of Google and Bitly. Now in this tutorial I will show you how to Shorten URL using Adfly API in PHP. It is one of the most popular URL shortener service provider. Also you can earn money through this service, that is the main reason of it’s popularity. It is providing an API to easily implement this service.

To get the API key, create an account on https://adf.ly and get your Adfly API key and User ID on http://adf.ly/publisher/tools#tools-api. You will found a link like below:

http://api.adf.ly/api.php?key=c3948ea56e6b536a0b945d1f55369e2c&uid=11775555&advert_type=int&domain=adf.ly&url=http://somewebsite.com

Here c3948ea56e6b536a0b945d1f55369e2c is your API key and 11775555 is your user ID.

/* Shorten Long URL */
function adflyShortenURL($url, $uid, $key){
	$apiURL = 'http://api.adf.ly/api.php?';

	// api queries
	$query = array(
		'key' => $key,
		'uid' => $uid,
		'advert_type' => 'int',
		'domain' => 'adf.ly'
	);

	// full api url with query string
	$apiURL = $apiURL . http_build_query($query).'&url='.$url;

	// get data
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $apiURL);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );

	$data = curl_exec($ch);
	curl_close($ch);
	return $data;

}

Now you can use above function as below:

$uid = '11775555';
$key = 'c3948ea56e6b536a0b945d1f55369e2c';

// Shorten a URL
echo adflyShortenURL("https://www.tricksofit.com", $uid, $key);

You will get the result of above like below:

http://adf.ly/1Rhq0n

I hope this tutorial will help you to shorten URL using Adfly API in PHP.