구글 단축 주소 정보
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
0
댓글 2개
마침 필요했었는데 감사합니다.
구글 단축 URL API 사용방법 (Javascript) : https://goo.gl/hU42mG