2010-05-19 2 views

답변

5

Ceejayoz의 예제에서 가면 한 줄로 만들 수 있습니다!

$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

글쎄, 기술적으로 PHP에서 하나의 라이너를 만들 수있다. 하지만 세미콜론과 같은 유효한 줄 구분 기호가있을 때 얻을 수 있습니다. 그 파이썬 가져 가라. –

7

나는 단지 googled 질문 : 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 실제로 haha는 google.com에 연결 되나요? – alex

+3

잘 모르시겠습니까? –

+0

wjat은 "yourapikey"입니까? 나는 오직 클라이언트 ID와 클라이언트 비밀만을 가지고있다. – user151496

3

그것은 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; 
관련 문제