Facebook Graph Api错误"发生了意外错误。请稍后重试您的请求"

时间:2017-08-24 07:45:12

标签: facebook facebook-graph-api facebook-javascript-sdk facebook-php-sdk

我试图检索Facebook群组中的所有成员收到此错误:

 array(5) {
    ["message"]=>
    string(66) "An unexpected error has occurred. Please retry your request later."
    ["type"]=>
    string(14) "OAuthException"
    ["is_transient"]=>
    bool(true)
    ["code"]=>
    int(2)
    ["fbtrace_id"]=>
    string(11) "AnfsXcdgM"
  }

这是我的代码:

$this->_facebook = new Facebook\Facebook(array('app_id' => "$app_id",'app_secret' => "$secret",'default_graph_version' => 'v2.10'));

$this->_facebook->setDefaultAccessToken($_SESSION['facebook_access_token']);

$query = "/".$groupID."/members?fields=id,name,link,picture,first_name,last_name";

try{
    $response = $this->_facebook->get($query);
    while($pagesEdge)
    {
     $pageDecoded = json_decode($pagesEdge);
     foreach($pageDecoded as $key => $member)
     {
         $id = $member->id;
     } 
    }
}catch (Facebook\Exceptions\FacebookResponseException $e) {  echo 'Graph returned an error: ' . $e->getMessage(); }

它适用于人数少的人群(即使是一个拥有10.000名成员的群体也是如此),但随机发生这种情况。

2 个答案:

答案 0 :(得分:0)

这可能是由服务器端超时引起的。当我请求大量数据时,我偶尔会收到此错误。也许您应该尝试使用limit参数限制您的请求(默认值应为25)。

答案 1 :(得分:0)

我通过执行一个cron来解决这个问题,该cron当时需要100个数据,并将下一个调用的令牌值放入文件文本中。

我在查询中添加此字符串,当$ url中的字段为空时,我退出执行

<?php 

public function updateGroupMembers($groupID)
{
    $tempNext = file_get_contents($this->dirM); //check if the next string token is in the file
    if (!empty($tempNext)) 
    {
        $queryUntil = $tempNext;
    }
    // Sets the default fallback access token so we don't have to pass it to each request
    $this->_facebook->setDefaultAccessToken($_SESSION['facebook_access_token']);
    // Create table name
    $tableName = $groupID . "_Members";

    // Query the Graph API to get all current member's ID and name
    try 
    {
        $query = "/".$groupID."/members?fields=id,name,link,picture,first_name,last_name".$queryUntil; //add the next string to my query
        $response = $this->_facebook->get($query); 
        $pagesEdge = $response->getGraphEdge();    
        // Index for the elements fetched from the API below
        $i = 0;
        // Get current time
        $pageDecoded = json_decode($pagesEdge);
        foreach($pageDecoded as $key => $member)
        {
           /* ...get data and process them... */
        }   

        $temp = $pagesEdge->getMetaData();
        $next = parse_url($temp['paging']['next']);
        parse_str($next['query'], $url);

        $access_token   = '&access_token='.$url['access_token'];
        $fields         = '&fields='.$url['fields'];
        $limit          = '&limit=100';
        $after          = '&after='.$url['after'];

        $res['until'] = $access_token.$fields.$limit.$after;

        file_put_contents($this->dirM, $res['until'], LOCK_EX);

        if ( empty($url['access_token']) || empty($url['fields']) || empty($url['limit']) || empty($url['after']) ) 
        {
            file_put_contents($this->dirM, '', LOCK_EX); //clean my txt file that contains my next string
            die('FINE');
        }

    } catch (Facebook\Exceptions\FacebookResponseException $e) {
        echo 'm2Graph returned an error: ' . $e->getMessage();
        exit;
    } catch (Facebook\Exceptions\FacebookSDKException $e) {
        echo 'm2Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
}