On my previous post I was shown how to shorten URL using Bitly API and PHP. In this post I will show you how you can shorten URL using Google URL shortener api using PHP. Google also provide URL shortener service (goo.gl) for its users. To working with this API you will need to get a Google URL Shorten API key. If you do not have get one from here for free.

Google URL Class:
/** * Google URL shorten API * @version API V1 */ class GoogleURL { /** * API URL * @var string */ private $apiURL = 'https://www.googleapis.com/urlshortener/v1/url'; /** * Google URL shorten Constructor * @param string $apiKey * @return void */ function __construct($apiKey) { # API URL with API keys $this->apiURL = $this->apiURL . '?key=' . $apiKey; } /** * Short Long URL * @param string $url as long URL * @return string as short URL or void */ public function encode($url) { $data = $this->cURL($url, true); return isset($data->id) ? $data->id : '' ; } /** * Extend Short URL * @param string $url as short URL * @return string as as long URL or void */ public function decode($url) { $data = $this->cURL($url, false); return isset($data->longUrl) ? $data->longUrl : '' ; } /** * Send cURL Request * @param string $url * @param bool $post * @return object */ private function cURL($url, $post = true) { # create cURL $ch = curl_init(); # POST request for URL shorten if ($post) { curl_setopt( $ch, CURLOPT_URL, $this->apiURL ); # set header content type for json curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: text/json') ); # set request method post curl_setopt( $ch, CURLOPT_POST, true ); # send json encoded request curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode(array('longUrl' => $url)) ); } # GET request for URL extend else { curl_setopt( $ch, CURLOPT_URL, $this->apiURL . '&shortUrl=' . $url ); } curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); #curl_setopt( $ch, CURLOPT_BINARYTRANSFER, true ); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); # execute curl request $json = curl_exec($ch); # close cURL connection curl_close($ch); # return response as object return (object) json_decode($json); } }
Usages:
$api = new GoogleURL('put_your_api_key_here'); # shorten url echo $api->encode('http://google.com'); # extend url echo $api->decode('http://goo.gl/mR2d');
Nice tutorial! Carry on......
ReplyDeleteAwesome work.Just wanted to drop a comment and say I am new to your blog and really like what I am reading.Thanks for the share
ReplyDeletedoes not generate the short url, I had to change the text / json for application / json
ReplyDeleteBoth will work!
DeleteI have this error whith your code and the Google Shorter URL api :
ReplyDeleteThis API does not support parsing form-encoded input.
there are a update, you need change
Deletecurl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: text/json') );
for ->
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json') );
please include those code which is said from Mr. Oscar Ivan Zamudio Montana.after that this code will run.............
DeleteDoes this also work with http://358.dk
ReplyDeleteit decodes short url but doesn't seem to shorten long urls? can any one tell me why?
ReplyDeleteLớp học kế toán tổng hợp thực hành tại hải phòng
ReplyDeleteLớp học kế toán tổng hợp thực hành tại biên hòa đồng nai
Lớp học kế toán tổng hợp thực hành tại vinh nghệ an
Lớp học kế toán tổng hợp thực hành tại hải dương
Lớp học kế toán tổng hợp thực hành tại ninh bình
Lớp học kế toán tổng hợp thực hành tại hưng yên
Lớp học kế toán tổng hợp thực hành tại phú thọ
Lớp học kế toán tổng hợp thực hành tại hà nam
Lớp học kế toán tổng hợp thực hành tại vĩnh phúc
Lớp học kế toán tổng hợp thực hành tại bắc giang
Lớp học kế toán tổng hợp thực hành tại thái nguyên
Lớp học kế toán tổng hợp thực hành tại thái bình
Lớp học kế toán tổng hợp thực hành tại nam định
Lớp học kế toán tổng hợp thực hành tại thanh hóa
Lớp học kế toán tổng hợp thực hành tại tphcm
Lớp học kế toán tổng hợp thực hành tại bắc ninh
Lớp học kế toán tổng hợp thực hành tại hà đông
Lớp học kế toán tổng hợp thực hành tại long biên
Lớp học kế toán tổng hợp thực hành tại thanh xuân
Lớp học kế toán tổng hợp thực hành tại cầu giấy
khóa học kế toán toán dành cho giám đốc và nhà quản lý
dịch vụ thành lập doanh nghiệp công ty trọn gói
dịch vụ thành lập doanh nghiệp công ty tại hải phòng
dịch vụ tạm ngừng, giải thể doanh nghiệp
dich vu ke toan thue dich vu lam bao cao tai chinh
This comment has been removed by the author.
ReplyDeleteThanks ,
ReplyDeleteyou need to change array('Content-Type: text/json') to array('Content-Type: application/json')
I have read your blog its very attractive and impressive. I like it your blog.
ReplyDeletePHP Training in chennai | PHP Training Course
PHP Training in chennai | Online PHP Course
I have read your blog its very attractive and impressive. I like it your blog.
ReplyDeletePHP Training in chennai | PHP Training Course
PHP Training in chennai | Online PHP Course
how to i include this to my website please help me
ReplyDeleteDid you know that you can create short links with AdFly and earn money from every click on your shortened urls.
ReplyDeleteThe blog or and best that is extremely useful to keep I can share the ideas of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.
ReplyDeleteanimal jam | five nights at freddy's | hotmail login
Thanks for looking professional services the list of Verified Foogle Packers And Movers. Compare to save money and select the best # www.fooglepackers.com
ReplyDeletePackers and Movers in Bangalore
Packers and Movers in Gurgaon
Packers and Movers in Pune
Thank for information visit top most articles list get instant quote from multiple:
ReplyDeleteCheapestpackers.co.in
Top Packers and Movers in Delhi
Top Packers and Movers in Noida
Top Packers and Movers in Faridabad
Top Packers and Movers in Ghaziabad
Top Packers and Movers in Gurgaon
Top Packers and Movers in Guwahati
m.google.com:ounnas abd el kader
ReplyDeleteInteresting, I wasn't aware of that context. I guess it makes some sense, thanks for the history lesson!
ReplyDeletewings io
Thanks for your post! Through your pen I found the problem up interesting! I believe there are many other people who are interested in them just like me! Thanks your shared!... I hope you will continue to have similar posts to share with everyone! I believe a lot of people will be surprised to read this article!
ReplyDeletevex 3
We are always shifting are goods according’s to 1bhk, 2bhk, 3bhk and 4bhk shifting services are provide www.awaypackersmovers.in of entire of a building’s. Packing and moving or car transportation and relocation of motor vehicles to the commercial or factories’ products for hassle expect done shifting services in India. Once the packing is done with almost for the loading is done with immense are done by team of experts while the boxes can be shifting of heavy machinery equipment’s providers solution’s by the folder services managements by moving and packing company. List of packer and mover in India, take at it happens to be persons switch that can intern commonly generally is an excellent for the Away a techniques associated with persons for the needs services in India.
ReplyDeleteAway Packers and Movers
Packers and Movers Bangalore
Packers and Movers Chennai
Packers and Movers Delhi
Packers and Movers Gurgaon
Packers and Movers Hyderabad
Packers and Movers Mumbai
Packers and Movers Pune
I believe there are many more pleasurable opportunities ahead for individuals that looked at your site. php training in chennai
ReplyDeleteIt is a very nice article including a lot of viral content. I am going to share it on social media. Get the Cold Pressed Oil in online.
ReplyDeleteHi, i really enjoyed to read your article.. i got clear idea through your views and ideas.. thanks for sharing your post..
ReplyDeleteMBA Project Center in Chennai | MBA Project Center in Velachery
Thank you for your amazing article.Its really useful form me..keep updating such an wonderful post..
ReplyDeleteMobile Computing Project Center in Chennai | Mobile Computing Projects in Velachery
I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post.
ReplyDeleteobatmataminus.aldotter.com
I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post.
ReplyDeleteobatkatarak.aldotter.com
obatsyarafkejepit99.aldotter.com
ReplyDeleteobatpenuruntrigliserida.idkes.com
ReplyDeleteobatsyarafkejepittradisional.blogspot.com/2017/12/berikut-manfaat-mengkonsumsi-kacang.html
ReplyDeleteavara-suksesbisnis.blogspot.com/2017/12/ketahui-olahraga-38-menit-sehari.html
ReplyDeleteThere are anything’s packing and goods better shifting services in Gurgaon, No big names with but, fame gained almost offering relocation enlisted on our website (Foogle Packers) and on top services of the entire growth keep dealing with Away experts trust upon of budget reasonable while assist a car and motor vehicles or installed from one location tracking with their products and also give for help to wonderful assist a decant of best packer and mover in Pune as well as process companies.
ReplyDeletehttp://www.fooglepackers.com/packersmoversbangalore.html
http://www.fooglepackers.com/packersmoversdelhi.html
http://www.fooglepackers.com/packersmoversgurgaon.html
obatglaukomaherbaltermutakhir.blogspot.com/2017/12/lawan-penyakit-mata-dengan-sayuran-hijau.html
ReplyDeleteSemoga informasinya membantu gan dan tunggu info lainnya dan salam sehat.
ReplyDeletehttp://obatinfeksikelaminwanita.walatragamat.com/
http://obatkeputihanherbal.walatragamat.com/
ReplyDeletehttp://obatkebaspadatangandankaki.blogspot.com/2018/01/gejala-kesemutan-dan-pegal-pegal.html
ReplyDeletehttp://tephoz2001-sinergibisnis2.blogspot.com/2018/01/penyakit-kelamin-pria-dan-mengapa-buah.html
Terimakasih atas informasinya dan jangan bosen-bosen mengunjungi webset kami ya gan.salam sehat
ReplyDeletehttp://obatkebaspadatangandankaki.blogspot.com/
http://obatinfeksikelaminwanita.walatragamat.com/
http://obathidrokel.walatragamat.com/
Terimakasih dengan infonya gan dan terus kunjungi di webset kami ya gan. salam sehat
ReplyDeleteobathidrokel.walatragamat.com
Terimakasih atas informasinya gan dan tunggu info selanjutnya di webset kami
ReplyDeletehttp://tephoz2001-sinergibisnis2.blogspot.com/2018/01/penyebab-dan-gejala-hidrokel.html
http://celclenz2u.blogspot.com/
ReplyDeleteVery good informative article. Thanks for sharing such nice article, keep on up dating such good articles.
ReplyDeleteAustere Technologies | Best Cloud Solution services
Nice article. Thanks for sharing.
ReplyDeleteNO.1 APP DEVELOPMENT SERVICES | MASSIL TECHNOLOGIES
Very nice information and explanation about PHP. Thanks for sharing. keep on updating such a nice information.
ReplyDeleteNO.1 IOT Services | INTERNET OF THINGS | Best IOT Services |
REALLY VERY EXCELLENT INFORMATION. I AM VERY GLAD TO SEE YOUR BLOG FOR THIS INFORMATION. THANKS FOR SHARING. KEEP UPDATING.
ReplyDeleteNO.1 Mobile APPilication DEVELOPMENT SERVICES | MASSIL TECHNOLOGIES
Thanks for the nice information and also it's very inspirational and Thanks for the detailed explanation. Really useful. Keep sharing more. Regards. Click Here for Commerce College in Hyderabad
ReplyDeleteWow...What an excellent informative blog, really helpful. Thank you. Best Oracle DBA Course Training| orskl
ReplyDeleteThis is really too useful and have more ideas from yours. keep sharing many things and thanks for sharing the information.
ReplyDeleteBest python training institute in Kanchipuram
Nice post... Really you are done a wonderful job. Thanks for sharing such wonderful information with us. Please keep on updating...
ReplyDeleteBest Java Training Academy in Kanchipuram
I have read your blog its very attractive and impressive. I like it your blog.
ReplyDeleteBest Software Testing Training Academy in Kanchipuram
Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
ReplyDeleteBest web Designing Training Training Institute in Kanchipuram
Nice information, want to know about Selenium Training In Chennai
ReplyDeleteSelenium Training In Chennai
Data Science Training In Chennai
Protractor Training in Chennai
jmeter training in chennai
Rpa Training Chennai
Rpa Course Chennai
Selenium Training institute In Chennai
Python Training In Chennai
Rpa Training in Chennai
ReplyDeleteRpa Course in Chennai
Blue prism training in Chennai
Data Science Training In Chennai
Data Science Course In Chennai
Data Science Course In Chennai
Please refer below if you are looking for Online Job Support and Proxy support from India
ReplyDeleteJava Online Job Support and Proxy support from India | AWS Online Job Support and Proxy Support From India | Python Online Job Support and Proxy Support From India | Angular Online Job Support from India | Android Online Job Support and Proxy Support from India
Thank you for excellent article.
Please refer below if you are looking for Online Job Support and Proxy support from India
ReplyDeleteJava Online Job Support and Proxy support from India | AWS Online Job Support and Proxy Support From India | Python Online Job Support and Proxy Support From India | Angular Online Job Support from India | Android Online Job Support and Proxy Support from India| SAP MM Online Job Support and Proxy support from India | ETL Testing Online Job Support and Proxy Support From India | SAP SD Online Job Support and Proxy Support From India
Thank you for excellent article.
Please refer below if you are looking for Online Job Support and Proxy support from India
ReplyDeleteHadoop Online Job Support and Proxy support from India | Informatica Online Job Support and Proxy Support From India | PHP Online Job Support and Proxy Support From India | DevOps Online Job Support from India | Selenium Online Job Support and Proxy Support from India| Manual Testing Online Job Support and Proxy Support From India | DotNet Online Job Support and Proxy Support from India
Thank you for excellent article.
php training in chennai
ReplyDeletephp online training in chennai
php training center in chennai
php class in chennai
php certification course
php training with placement in chennai
appium online training
ReplyDeleteappium training in chennai
appium training institutes in chennai
appium training in chennai
best appium training institute in chennai
best training institutes for appium in chennai
It's really worth reading the blog post whcih has useful content. I have recently done CRS Info Solutions which is highly helpful to upgrade my career. What is the Salesforce course training fee in Chennai? Which is the best option from Salesforce training in Chennai? Who offers job oriented Salesforce training in Chennai? Find the best institute for project based Salesforce training in Chennai.
ReplyDeleteThis code is really useful. I have recently edited my Anish College .
ReplyDeleteThanks for the post. It was very interesting and meaningful. I really appreciate it! Keep updating stuff like this.
ReplyDeleteOnline Data Science Classes
Selenium Training in Pune
AWS Online Classes
Python Online Classes