프로그램.코딩
구글 짧은주소 얻기함수
landzz
2013. 2. 8. 14:41
긴도메인을 짧은주소로 얻는 구글API
function googl_short_url($long_url) {
$googl_url = "https://www.googleapis.com/urlshortener/v1/url";
$post_data = array('longUrl' => $long_url);
$headers = array('Content-Type:application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $googl_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($post_data));
$result = curl_exec($ch);
curl_close($ch);
//print_r2($result);
$obj = json_decode($result);
$short_url = $obj->{'id'};
return $short_url;
}