<?php
class GoogleShortUrlHooks
{
// 替换短网址
public function getShortUrl($url,$apikey)
{
if(!$apikey) return $url;

return $this->request($url,$apikey);
} private function request($url,$apikey){
if(!$url) return '';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://goo.gl/api/url');   //goo.gl api url
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, '[email protected]&url='.$url.'&auth_token='.$this->googlToken($url));
$short = curl_exec($curl);
$short = json_decode($short);
curl_close($curl);
if($short->short_url){
    return $short->short_url;
}else{
return $url;
}
} private function googlToken($b){
$i = $this->tke($b);
$i = $i >> 2 & 1073741823;
$i = $i >> 4 & 67108800 | $i & 63;
$i = $i >> 4 & 4193280 | $i & 1023;
$i = $i >> 4 & 245760 | $i & 16383;
$j = "7";
$h = $this->tkf($b);
$k = ($i >> 2 & 15) << 4 | $h & 15;
$k |= ($i >> 6 & 15) << 12 | ($h >> 8 & 15) << 8;
$k |= ($i >> 10 & 15) << 20 | ($h >> 16 & 15) << 16;
$k |= ($i >> 14 & 15) << 28 | ($h >> 24 & 15) << 24;
$j .= $this->tkd($k);
return $j;
} private function tkc(){
$l = 0;
foreach (func_get_args() as $val) {
$val &= 4294967295;
$val += $val > 2147483647 ? -4294967296 : ($val < -2147483647 ? 4294967296 : 0);
$l   += $val;
$l   += $l > 2147483647 ? -4294967296 : ($l < -2147483647 ? 4294967296 : 0);
}
return $l;
} private function tkd($l){
$l = $l > 0 ? $l : $l + 4294967296;
$m = "$l";  //must be a string
$o = 0;
$n = false;
for($p = strlen($m) - 1; $p >= 0; --$p){
$q = $m[$p];
if($n){
$q *= 2;
$o += floor($q / 10) + $q % 10;
} else {
$o += $q;
}
$n = !$n;
}
$m = $o % 10;
$o = 0;
if($m != 0){
$o = 10 - $m;
if(strlen($l) % 2 == 1){
if ($o % 2 == 1){
$o += 9;
}
$o /= 2;
}
}
return "$o$l";
} private function tke($l){
$m = 5381;
for($o = 0; $o < strlen($l); $o++){
$m = $this->tkc($m << 5, $m, ord($l[$o]));
}
return $m;
} private function tkf($l){
$m = 0;
for($o = 0; $o < strlen($l); $o++){
$m = $this->tkc(ord($l[$o]), $m << 6, $m << 16, -$m);
}
return $m;
}
}
$shorten = new GoogleShortUrlHooks;
 $short = $shorten->getShortUrl("http://www.17zencart.com",'AIzaSyBmPhgDPZA3​​ZQuS3VQo_aq2xv0mmsznXKU');
 echo $short;这个类是别人写的  $apikey=AIzaSyBmPhgDPZA3​​ZQuS3VQo_aq2xv0mmsznXKU 这个是申请的 
1.结果还是原网址
2.$short->short_url 是空的
3.$apikey的值并没有用到  

解决方案 »

  1.   


    <?php
    class SinaShortUrlHooks
    {
    // 替换短网址
    public function getShortUrl($url,$appkey)
    {
    if(!$appkey) return $url;
    return $this->request($url,$appkey);
    } private function request($url,$appkey){
    if(!$url) return '';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, 'http://api.t.sina.com.cn/short_url/shorten.json');   //goo.gl api url
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, 'source='.$appkey.'&url_long='.urlencode($url));
    $short = curl_exec($curl);
    $short = json_decode($short);
    curl_close($curl);
    if(!isset($short->error_code)){
    return $short['0']->url_short;
    }else{
    return $url;
    }
    }
    }
    $a = new SinaShortUrlHooks;
    echo $a->getShortUrl("http://17zencart.com",3585127787);新浪的可以 但是要求用谷歌的
      

  2.   


    <?php
    function shortenGoogleUrl($long_url){
     $apiKey='AIzaSyBmPhgDPZA3​​ZQuS3VQo_aq2xv0mmsznXKU';//Get API key from : http://code.google.com/apis/console/
     $postData=array('longUrl'=>$long_url,'key'=>$apiKey);
     $jsonData=json_encode($postData);
     //echo $jsonData;
     $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;
    }
    echo shortenGoogleUrl('http://17zencart.com');
    ?>
    搞定了~