tcelestino

Encurtador de URL API bit.ly

Jan 23rd, 2012
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. <?php
  2. //função do curl
  3. function curl_get_result($url) {
  4. $ch = curl_init($url);
  5. $timeout = 50;
  6. curl_setopt($ch,CURLOPT_URL,$url);
  7. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  8. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  9. $data = curl_exec($ch);
  10. curl_close($ch);
  11. return $data;
  12. }
  13. function shortURL($link) {
  14. $format = "json"; //formato da URL
  15. $login = ""; // digite o login do bit.ly
  16. $apiKey = ""; //insira a key da api (http://bitly.com/a/your_api_key/)
  17.  
  18. //pega as informações e passa para a API
  19. $bitly = 'https://api-ssl.bitly.com/v3/shorten?login='.$login.'&apiKey='.$apiKey.'&longUrl='.urldecode($link).'&format='.$format;
  20.  
  21. $getURL = curl_get_result($bitly);
  22.  
  23. $json = @json_decode($getURL, true);
  24.  
  25. return $json['data']['url'];
  26.  
  27. }
  28. echo shortURL("http://google.com");
  29. ?>
Add Comment
Please, Sign In to add comment