Bitly is another great URL Shortening service. Bitly shortens more than one billion links per month for use with websites, social networks, SMS and Email. It became popular when it became default URL Shortening Service for Twitter in 2009. Now we will learn how to integrate Bitly into any PHP application.
Here is a simple PHP class that uses The Bitly API. It shorten URLs and also convert short URLs to original ones.
Get oAuth Token:
Bitly requires you to generate generic oAuth token to shorten URL using API.
- Signup/login at http://bitly.com.
- Now to generate generic oAuth token click here.
- At bottom of the page confirm your account password and Bitly will generate oAuth token.
- Copy this oAuth token, it will be used in below Bitly PHP Class.
Bitly PHP Class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<?php /** * This file is part of bit-ly-php * * copyright: http://earlysandwich.com/programming/php/bitly-url-shortner-php-class-572/ * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ class bitly{ private $target; private $token; function __construct($token = null) { if ( $token != null ) { $this->token = $token; } } public function curl($APIurl) { $ch = curl_init(); $timeout = 5; curl_setopt($ch,CURLOPT_URL,$APIurl); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); $data = curl_exec($ch); curl_close($ch); return $data; } public function shorten($url ,$format='txt') { # Set Google Shortener API target $this->target = 'https://api-ssl.bitly.com/v3/shorten?'; # Set API key if available $this->target .= 'access_token='.$this->token; $this->target .= '&longUrl=' . urlencode($url); $this->target .= '&format=' . $format; $short_url = $this->curl($this->target); if ( $format == 'json') { return json_decode($short_url); } elseif($format == 'txt') { return $short_url; } } public function expand($url ,$format='txt') { # Set Google Shortener API target $this->target = 'https://api-ssl.bitly.com/v3/expand?'; # Set API key if available $this->target .= 'access_token='.$this->token; $this->target .= '&shortUrl=' . urlencode($url); $this->target .= '&format=' . $format; $short_url = $this->curl($this->target); if ( $format == 'json') { return json_decode($short_url); } elseif($format == 'txt') { return $short_url; } } } ?> |
Usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?PHP require_once('bitly-class.php'); // Code for shortening URL $bitly = new bitly('{{OAuth access_token}}'); // Shorten URL $short_url = $bitly->shorten("http://earlysandwich.com"); echo "Short URL is: " . $short_url; // Code for Expanding URL $bitly = new bitly('{{OAuth access_token}}'); // Expand URL $long_url = $bitly->expand("bitly.com/earlysan"); echo "Long URL is: " . $long_url; ?> |