Created
March 8, 2013 19:34
-
-
Save tcelestino/5119170 to your computer and use it in GitHub Desktop.
shorten url using bit.ly api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//função do curl baseado no script do David Walsh (http://davidwalsh.name/) | |
function curl_get_result($url) { | |
$ch = curl_init($url); | |
$timeout = 50; | |
curl_setopt($ch,CURLOPT_URL,$url); | |
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); | |
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); | |
$data = curl_exec($ch); | |
curl_close($ch); | |
return $data; | |
} | |
function shortURL($link) { | |
$format = "json"; | |
$login = "your-login"; | |
$apiKey = "insert_your_apikey"; | |
$bitly = 'https://api-ssl.bitly.com/v3/shorten?login='.$login.'&apiKey='.$apiKey.'&longUrl='.urldecode($link).'&format='.$format; | |
$getURL = curl_get_result($bitly); | |
$json = @json_decode($getURL, true); | |
return $json['data']['url']; | |
} | |
echo shortURL("http://google.com"); // example |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment