Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Encurtador de URL usando a API do Bit.ly e PHP cURL
- Como usar: <?php bitly(); ?> ~> coloque no local onde quer que apareรงa a URL encurtada.
- */
- function bitly()
- {
- global $post;
- //login information
- $url = get_permalink($post->ID); //generates wordpress' permalink
- $login = 'YOUR_LOGIN'; //your bit.ly login
- $apikey = 'YOUR_BITLI_API_KEY'; //bit.ly apikey - quintaldosamba
- $format = 'json'; //choose between json or xml
- $version = '2.0.1';
- //create the URL
- $bitly = 'http://api.bit.ly/shorten';
- $param = 'version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$apikey.'&format='.$format;
- //get the url
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $bitly . "?" . $param);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $response = curl_exec($ch);
- curl_close($ch);
- //parse depending on desired format
- if(strtolower($format) == 'json')
- {
- $json = @json_decode($response,true);
- echo $json['results'][$url]['shortUrl'];
- }
- else //xml
- {
- $xml = simplexml_load_string($response);
- echo 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
- }
- }
Add Comment
Please, Sign In to add comment