구글 단축 주소 > 개발자팁

개발자팁

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

구글 단축 주소 정보

PHP 구글 단축 주소

본문


<? 
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";
?> 
 
추천
0
  • 복사

댓글 2개

© SIRSOFT
현재 페이지 제일 처음으로