Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function bitly()
- {
- //login information
- $url = get_permalink(); //generates wordpress' permalink
- $login = 'SEU_BIT_LY_LOGIN'; //your bit.ly login
- $apikey = 'SUA_API_KEY_VAI_AQUI'; //bit.ly apikey
- $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;
- }
- }
- //Botão do Twitter ficará assim:
- <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