Engineering/PHP

goo.gl short URL API 사용

부스 boos 2011. 3. 11. 17:46
728x90

구글 사이트를 참조합니다.
http://code.google.com/apis/urlshortener/v1/reference.html#method_urlshortener_url_insert  

2011년 3월 기준 위 링크의 API 를 참조했습니다.

function googl_short_url($long_url) {
//  global $googl_login, $googl_api_key;
    $googl_api_key = "API키값";
    $googl_url = "https://www.googleapis.com/urlshortener/v1/url";
    $post_url = $googl_url . "?key=" . $googl_api_key;
    $post_data = "{\"longUrl\": \"".$long_url."\"}";

    $response = file_post_contents_curl($post_url, $post_data);
    $obj = json_decode($response);
    $short_url = $obj->{'id'};

    return $short_url;


$long_url 값, 즉 URL 주소를 입력하면 ( 예) http://www.google.co.kr/intl/ko/about.html),
"http://goo.gl/tuaw2" 가 $short_url 이 됩니다.