0

所以我有一个小的 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 个月前开始使用这个脚本时,我没有这个错误,但现在我开始得到它,我不知道为什么,所以如果有人有任何想法,我会非常感激。

更新: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" }}

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

4

2 回答 2

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。

于 2015-01-14T09:22:12.960 回答
0

有时由于网络 curl 没有在 30 秒内返回响应(php 中的默认时间限制以完成 cmd)

尝试更改 php.ini 中的时间限制,或者如果这不在您的控制范围内,或者您不想为所有 php cmds 修改它,请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" 
}
}

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

希望这可以帮助

于 2015-01-14T09:04:58.393 回答