03.04.10 - Short URL's through j.mp or bit.ly API with PHP

How to short URL's through j.mp or bit.ly API with a simple PHP function and cURL

First, in order to use the bit.ly API, you must sign up and get an api key. Once done, with cURL we can short a URL calling the next simple PHP function:
function shortUrl ($url)
{
  $bitlyUser = 'TEST';
  $apiKey = 'API_KEY';

  $ch = curl_init();

  //cURL options
  curl_setopt($ch, CURLOPT_URL, 'http://api.j.mp/v3/shorten?login=' . $bitlyUser . '&apiKey=' . $apiKey .'&uri=' . htmlspecialchars($url) . '&format=txt');
  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  //Execution
  $ret = curl_exec($ch);
  curl_close($ch);
  return $ret;
}
More news about:  php
Tags:  short url,  api,  bitly,  curl

Comments

There are no comments jet. Be the first to comment!

Comment