Url shortening is attached with many benefits like social media friendliness and micro blogging usability, better SEO compatibility, ease of sharing, analytic data grossing, and most importantly the shortened URLs look more sober to eyes than usual orthodox long URLs.
There are many names, providing URL shortening services. Amongst all, Bitly is the most popular because of its reliability and other useful features. In this tutorial we will learn URL shortening using Bitly API
Steps to Create Shorten URL Using Bitly API
Firstly, create an account on Bitly by going to URL below:
After registering on Bitly ,you need to create an app on Bitly.
Please go to the url:
and click My Apps -> Manage My apps and then Register an application.
Now, you must have an access token to make an api call. For this click Get Registration Code .
You will receive an email on your registered email address, containing a link to take you for registering your first application.
Just fill the required details and click Create Your App button. You will be redirected to page showing client Id and client secret token for your application.
Again insert your password to generate access token.
Now use this access token in the following code to make you first call to Bitly API.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php $access_token = '**** INSERT ACCESS TOKEN HERE ****'; $long_url='http://lifehacker.com/can-i-really-make-a-living-by-blogging-1537783554'; $url = 'https://api-ssl.bitly.com/v3/shorten?access_token='.$access_token.'&longUrl='.$long_url; $ch1 = curl_init($url); //open connection curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 0 ); curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0 ); curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1 ); $result = curl_exec($ch1); //execute post $data_array = json_decode($result); echo "<pre>"; print_r($newdata); echo 'Long url: '.$long_url.'<br>'; echo 'shortened url: '.$data_array->data->url; ?> |
Using above php code you can easily implement Bitly API to shorten your web post url.
Hi. Although I am very new to PHP, I managed to generate a Bitly URL using the codes you provided above.
Unfortunately, I am just one step away from success. Bitly doesn’t seem to recognize the “&” characters in URL.
The long URL parsed to Bitly is:
http://mydomain.com/calculator?TokenA=APPLE&PriceA=5.061&TokenB=ORANGE&PriceB=3.02&QuantityB=90.44&PriceAend=7.76&PriceBend=2.287&RewardPrice=2.2&RewardQuantity=14.88&submit=SHOW+ME+MONEY
When I clicked on the shortened URL, it went to:
http://mydomain.com/calculator?TokenA=SHIB
And because of this error, the calculator page failed to show the proper result. It seems that something is not right with the “&” character.