google shorten api

申请ApiKey地址:https://code.google.com/apis/console

<?php
function shortenGoogleUrl($long_url){
 $apiKey='API-KEY';//Get API key from : http://code.google.com/apis/console/
 $postData=array('longUrl'=>$long_url,'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);
 curl_close($curlObj);
 $json=json_decode($response);
 return$json->id;
}
?><?php
function expandGoogleUrl($short_url){
 $curlObj=curl_init();
 curl_setopt($curlObj, CURLOPT_URL,'https://www.googleapis.com/urlshortener/v1/url?shortUrl='.$short_url);
 curl_setopt($curlObj, CURLOPT_HEADER,0);
 curl_setopt($curlObj, CURLOPT_RETURNTRANSFER,1);
 curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER,0);
 $response=curl_exec($curlObj);
 curl_close($curlObj);
 $json=json_decode($response);
 return$json->longUrl;
}
?>



你可能感兴趣的:(google shorten api)