2010-05-19 2 views

Répondre

5

En partant de l'exemple de Ceejayoz, vous pouvez en faire un seul revêtement!

$short_url = json_decode(file_get_contents("http://api.bit.ly/v3/shorten?login=bitlyusername&apiKey=bitlyapikey&longUrl=".urlencode("http://example.com")."&format=json"))->data->url; 
+4

Eh bien, techniquement, vous pouvez faire n'importe quoi dans PHP un seul paquebot. Mais c'est ce que vous obtenez lorsque vous avez un délimiteur de ligne valide comme le point-virgule. Prenez ce Python! –

7

Je viens googled votre question: Example code

/* Example code */ 
$link = "http://www.stackoverflow.com"; 

print getSmallLink($link); 

function getSmallLink($longurl){ 
// Bit.ly 
$url = "http://api.bit.ly/shorten?version=2.0.1&longUrl=$longurl&login=YOURLOGIN&apiKey=YOURAPIKEY&format=json&history=1"; 

$s = curl_init(); 
curl_setopt($s,CURLOPT_URL, $url); 
curl_setopt($s,CURLOPT_HEADER,false); 
curl_setopt($s,CURLOPT_RETURNTRANSFER,1); 
$result = curl_exec($s); 
curl_close($s); 

$obj = json_decode($result, true); 
return $obj["results"]["$longurl"]["shortUrl"]; 
} 
+4

+1, avez-vous réellement * lié * à google.com? – alex

+3

bien, vous ne savez jamais ;-) –

+0

wjat est "yourapikey"? J'ai seulement reçu l'ID du client et le secret du client – user151496

3

C'est un very simple API.

$long_url = urlencode('http://example.com/'); 

$bitly_login = 'username'; 
$bitly_apikey = 'YOUR API KEY'; 

$bitly_response = json_decode(file_get_contents("http://api.bit.ly/v3/shorten?login={$bitly_login}&apiKey={$bitly_apikey}&longUrl={$long_url}&format=json")); 

$short_url = $bitly_response->data->url;