치즈버거

구글 단축 주소

· 2017-01-10 (화) 12:56:24 · 4086 · 2

[code]

<? 

class googl {
    function googl($key,$apiURL = 'https://www.googleapis.com/urlshortener/v1/url') {
        $this->apiURL = $apiURL.'?key='.$key;
    }
    function shorten($url) {
        $response = $this->send($url);
        return isset($response['id']) ? $response['id'] : false;
    }
    function expand($url) {
        $response = $this->send($url,false);
        return isset($response['longUrl']) ? $response['longUrl'] : false;
    }
    function send($url,$shorten = true) {
        $ch = curl_init();
        if($shorten) {
            curl_setopt($ch,CURLOPT_URL,$this->apiURL);
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode(array("longUrl"=>$url)));
            curl_setopt($ch,CURLOPT_HTTPHEADER,array("Content-Type: application/json"));
        }
        else {
            curl_setopt($ch,CURLOPT_URL,$this->apiURL.'&shortUrl='.$url);
        }
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        $result = curl_exec($ch);
        curl_close($ch);
        return json_decode($result,true);
    }       
}

$key = $config[cf_googl_shorturl_apikey]; // 구글 API Key
$googl = new googl($key);
//$sUrl = googl_short_url($url);
$sUrl = $googl->shorten("http://multime.kr/");
$eUrl = $googl->expand($sUrl);
echo "$sUrl<br>$eUrl";

?> 

[/code] 

|

댓글 2개

마침 필요했었는데 감사합니다.
구글 단축 URL API 사용방법 (Javascript) : https://goo.gl/hU42mG
댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

5,403건

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
PHP 17-01-13 조회 2,908
PHP 17-01-12 조회 2,433
PHP 17-01-12 조회 2,695
PHP 17-01-12 조회 2,603
PHP 17-01-11 조회 2,905
PHP 17-01-11 조회 2,644
PHP 17-01-11 조회 2,521
PHP 17-01-10 조회 2,569
PHP 17-01-10 조회 2,310
PHP 17-01-10 조회 2,438
PHP 17-01-10 조회 4,087
PHP 17-01-09 조회 4,677
PHP 17-01-09 조회 3,380
PHP 17-01-09 조회 2,756
PHP 17-01-08 조회 4,641
PHP 17-01-08 조회 2,745
PHP 17-01-08 조회 2,584
PHP 17-01-07 조회 4,956
PHP 17-01-07 조회 3,230
PHP 17-01-06 조회 2,546
PHP 17-01-06 조회 2,979
PHP 17-01-06 조회 2,718
PHP 17-01-06 조회 3,099
PHP 17-01-05 조회 2,618
PHP 17-01-05 조회 2,971
PHP 17-01-05 조회 2,668
PHP 17-01-04 조회 2,830
PHP 17-01-04 조회 2,592
PHP 17-01-04 조회 3,008
PHP 17-01-04 조회 3,101