본문 바로가기

Web/PHP

구글 단축 주소

반응형

출처 : http://multime.kr/bbs/board.php?bo_table=tech&wr_id=51

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]; $googl = new googl($key); //$sUrl = googl_short_url($url); $sUrl = $googl->shorten("http://multime.kr/"); $eUrl = $googl->expand($sUrl); echo "$sUrl<br>$eUrl";

반응형