본문 바로가기

TroubleShooting/PHP

goo.gl short URL API 사용

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 이 됩니다.

'TroubleShooting > PHP' 카테고리의 다른 글

apm 설치  (0) 2011.11.14
PHP함수 call_user_func_array()  (0) 2011.07.12
HTML 안의 php 코드 인식  (0) 2011.05.27
php 문자열을 파일로 저장하는 예제  (0) 2011.02.26
PHP XML 지원 (--disable-dom 를 나올때)  (0) 2011.02.21