Advertisement
miriamdepaula

Criando botão para publicar no twitter, com short url

May 14th, 2011
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.17 KB | None | 0 0
  1. function bitly()
  2. {
  3.     //login information
  4.     $url = get_permalink();  //generates wordpress' permalink
  5.     $login = 'SEU_BIT_LY_LOGIN';    //your bit.ly login
  6.     $apikey = 'SUA_API_KEY_VAI_AQUI'; //bit.ly apikey
  7.     $format = 'json';   //choose between json or xml
  8.     $version = '2.0.1';
  9.  
  10.     //create the URL
  11.     $bitly = 'http://api.bit.ly/shorten';
  12.     $param = 'version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$apikey.'&format='.$format;
  13.  
  14.     //get the url
  15.     $ch = curl_init();
  16.     curl_setopt($ch, CURLOPT_URL, $bitly . "?" . $param);
  17.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  18.     $response = curl_exec($ch);
  19.     curl_close($ch);
  20.  
  21.     //parse depending on desired format
  22.     if(strtolower($format) == 'json')
  23.     {
  24.         $json = @json_decode($response,true);
  25.         echo $json['results'][$url]['shortUrl'];
  26.     }
  27.     else //xml
  28.     {
  29.         $xml = simplexml_load_string($response);
  30.         echo 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
  31.     }
  32. }
  33.  
  34. //Botão do Twitter ficará assim:
  35.  
  36. <a href="http://twitter.com/home?status=Vale a pena conferir: <?php bitly(); ?> (via @miriamdepaula)" title="Clique aqui para publicar este post no Twitter!" target="_blank" class="twitter-icon">Publicar no Twitter</a>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement