Create shortcut links using Google API

Create shortcut links using Google API

I needed a short link, so I made it PHP.

You can use it after registering Google API.

How to use: Shortur_google ('original address', 'API key');
<?php
	function shorturl_google($url, $key)
	{
		$post = array('longUrl' => $url);
		$json = json_encode($post);

		$curlObj = curl_init();

		curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?key='.$key);
		curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($curlObj, CURLOPT_HEADER, 0);
		curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
		curl_setopt($curlObj, CURLOPT_POST, 1);
		curl_setopt($curlObj, CURLOPT_POSTFIELDS, $json);

		$json = json_decode(curl_exec($curlObj));

		curl_close($curlObj);

		return $json->id;
	}

	echo shorturl_google('http://kalmuri.kilho.net/', 'API-KEY-API-KEY-API-KEY');
?>

Leave a Reply

이메일 주소는 공개되지 않습니다. (* 질문, 건의사항 등은 "질문게시판"을 이용해주세요)