Shorten URL using Google URL Shortener API:
01 <?php |
|
02 | $longUrl = 'http://facebook.com' ; |
03 | $apiKey = 'Your Api Key here' ; |
04 | //You can get API key here : http://code.google.com/apis/console/ |
05 |
06 | $postData = array ( 'longUrl' => $longUrl , 'key' => $apiKey ); |
07 | $jsonData = json_encode( $postData ); |
08 |
09 | $curlObj = curl_init(); |
10 |
11 | curl_setopt( $curlObj , CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url' ); |
12 |
13 | curl_setopt( $curlObj , CURLOPT_RETURNTRANSFER, 1); |
14 |
15 | curl_setopt( $curlObj , CURLOPT_SSL_VERIFYPEER, 0); |
16 |
17 | curl_setopt( $curlObj , CURLOPT_HEADER, 0); |
18 |
19 | curl_setopt( $curlObj , CURLOPT_HTTPHEADER, array ( 'Content-type:application/json' )); |
20 |
21 | curl_setopt( $curlObj , CURLOPT_POST, 1); |
22 |
23 | curl_setopt( $curlObj , CURLOPT_POSTFIELDS, $jsonData ); |
24 |
25 | $response = curl_exec( $curlObj ); |
26 |
27 | $json = json_decode( $response ); |
28 |
29 | curl_close( $curlObj ); |
30 | echo 'Shortened URL ->' . $json ->id; |
31 | ?> |
No comments:
Post a Comment