谷歌缩短版问题

时间:2015-01-14 08:46:16

标签: php goo.gl

所以我有一个小的PHP脚本来生成短链接,它工作,但有时我得到这个错误:

  

未定义的属性:stdClass :: $ id"," file":ShortLink.php"," line":31

这是我的剧本:

<?php

class ShortLink {

public static function generateShortLink($longUrl)
{
    //This is the URL you want to shorten
    $apiKey = 'MY_API_KEY';
    //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);

    //change the response json string to object
    $json = json_decode($response);

    curl_close($curlObj);

    return $json->id;
   }

 }

当我6或7个月前开始使用这个脚本时,我没有出现这个错误但现在我开始得到它并且我不知道为什么,所以如果有人知道我会非常感激。< / p>

更新 当我vardump我的$json我明白了:

{ ["domain"]=> string(11) "usageLimits" ["reason"]=> string(26) "userRateLimitExceededUnreg" ["message"]=> string(40) "User Rate Limit Exceeded. Please sign up" ["extendedHelp"]=> string(36) "https://code.google.com/apis/console" } } ["code"]=> int(403) ["message"]=> string(40) "User Rate Limit Exceeded. Please sign up" }}

所以我想知道谷歌是否限制谷歌缩短服务?

2 个答案:

答案 0 :(得分:1)

class ShortLink { 
public static function generateShortLink($longUrl)
{
    //This is the URL you want to shorten
    $apiKey = 'YOUR_SERVER_API_KEY';
    //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);

    //change the response json string to object
    $json = json_decode($response);

    curl_close($curlObj);
    if(!is_object($json))  
    {  
        return(false);  
    }  
    return $json->id;
   }

}
$api = new ShortLink();
$shorturlid=$api->generateShortLink('http://avecsrthgdgnb.avcd');
echo $shorturlid;

您使用新的控制台,然后启用URL Shortener API。

答案 1 :(得分:0)

有时因为网络卷曲不会在30秒内返回响应(php中的默认时间限制为cmd完成)

尝试更改php.ini中的时间限制,或者如果它不在您的控件中,或者您不想为所有php cmd修改它,请在致电bool set_time_limit ( int $seconds )之前尝试curl_exec

<强>更新

我看到返回的json中没有id字段。

{ ["domain"]=> string(11) "usageLimits" 
  ["reason"]=> string(26) "userRateLimitExceededUnreg" 
  ["message"]=> string(40) "User Rate Limit Exceeded. Please sign up" 
  ["extendedHelp"]=> string(36) "https://code.google.com/apis/console" 
} 
}  
  ["code"]=> int(403) 
  ["message"]=> string(40) "User Rate Limit Exceeded. Please sign up" 
}
}

如果您仔细查看了用户速率限制See here,请参阅有关使用Google API的限制的详细信息。 (您可以尝试不同的IP,即不同的机器或不同的api密钥,相同的代码可能会开始工作)。

希望这有帮助