<?php

class GoogleURL {

	public static function short($longUrl) {
		$apikey		 = 'AIzaSyCH64im1tQ6pjHDegTmWxFH3gBajdu41JE';
		$endpoint	 = 'https://www.googleapis.com/urlshortener/v1';

		$ch					 = curl_init(sprintf('%s/url?key=%s', $endpoint, $apikey));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$requestData = array('longUrl' => $longUrl);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData));
		$result			 = curl_exec($ch);
		$result = json_decode($result, true);
		return $result['id'];
	}

}
