2010-07-19 5 views
0

다른 헤드 라인과 링크를 표시하는 PHP 루프가 있습니다. Bitly를 통해 자동 생성 된 짧은 링크로 Twitter에 대한 링크를 보내고 싶습니다. 이 링크가 어떻게 구성되어 있습니까 :트위터에 보내기 전에 링크를 짧게하십시오.

<a href="http://twitter.com/home?status=This is the headling {dynamic Permalink}">Share on Twitter</a> 

루프가 실제 퍼 뮤 링크를 만든 후에 어떻게 단축 링크를 만들 수 있습니까?

답변

1

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

+0

도움 주셔서 감사합니다. 하지만 실제 페이지에 제공 한 코드를 통합하는 방법을 잘 모르는 경우가 있습니다. 전체 예제를 통해 나를 기꺼이 도울 수 있습니까? – user55655

+0

통합이 매우 간단합니다. 이 함수를 코드 상단의 어딘가에 추가하면 다음과 같이 사용할 수 있습니다. make_bitly_url ($ url, $ login, $ apiKey); – Mike

관련 문제