<form method="post">
URL : <input type="text" id="url" name="url" required><span id="error"></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit'])) {
$apiURL = 'https://www.googleapis.com/urlshortener/v1/url';
$key = 'AIzaSyCEnEb17VbKbtR_9jp36i5mYZLSAowVICk'; // GOOGLE API KEY
$apiURL = $apiURL.'?key='.$key;
$url = $_POST['url'];
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$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"));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result,true);
//print_r($result['id']);
echo 'Short URL : <a href="'.$result['id'].'" style="color:red";" target="blank">'.$result['id'].'</a><br><br>';
}
?>