bit.ly에 등록하고 API 키를 가져와야합니다.
function make_bitly_url($url, $login, $apiKey, $format = 'xml',$version = '2.0.1', $history=1) {
if(substr($url, 0, 7) != 'http://')
$url = 'http://'.$url;
$bitly = 'http://api.bit.ly/shorten';
$param = 'version='.$version.'&longUrl='.urlencode($url).'&login='
.$login.'&apiKey='.$apiKey.'&format='.$format.'&history='.$history;
//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);
return $json['results'][$url]['shortUrl'];
} else {
$xml = simplexml_load_string($response);
return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
} // end: function
이제 $ 로그인 변수는 짧게하려면, 로그인은 $ apeKey 당신의 apiKey에와 $ URL이 긴 URL입니다 및 함수는 짧은 bit.ly 주소를 출력합니다.
기타 정보 : http://code.google.com/p/bitly-api/wiki/ApiDocumentation
도움 주셔서 감사합니다. 하지만 실제 페이지에 제공 한 코드를 통합하는 방법을 잘 모르는 경우가 있습니다. 전체 예제를 통해 나를 기꺼이 도울 수 있습니까? – user55655
통합이 매우 간단합니다. 이 함수를 코드 상단의 어딘가에 추가하면 다음과 같이 사용할 수 있습니다. make_bitly_url ($ url, $ login, $ apiKey); – Mike