forked from dilab/url-shortener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.php
45 lines (36 loc) · 1.17 KB
/
gen.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/**
*@author XuDing
*@email hello@startutorial.com
*@website http://www.startutorial.com
**/
//process only if data is posted
if(isset($_REQUEST['url'])&&!empty($_REQUEST['url'])){
//This is the URL you want to shorten
$longUrl = $_REQUEST['url'];
$apiKey = 'AIzaSyC-au_vqcST44BnxRIRCjy1eIcFwrJcSh4';
//Get API key from : http://code.google.com/apis/console/
$postData = array('longUrl' => $longUrl, 'key' => $apiKey);
$jsonData = json_encode($postData);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
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, $jsonData);
$response = curl_exec($curlObj);
//convert reponse to a json object
$json = json_decode($response);
curl_close($curlObj);
//echo result
if(isset($json->error)){
echo $json->error->message;
}else{
echo $json->id;
}
}else{
echo 'Please enter a URL';
}
?>