skip to Main Content

I’m trying to retrieve all members in a Facebook group getting this error:

 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"
  }

Here is my code:

$this->_facebook = new FacebookFacebook(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 (FacebookExceptionsFacebookResponseException $e) {  echo 'Graph returned an error: ' . $e->getMessage(); }

It works for groups with few hundreads of people (even once for a group with 10.000 members) but randomly I’m occurring to this.

2

Answers


  1. Chosen as BEST ANSWER

    I solved this by doing a cron that takes 100 data at the time and putting into a file text the value of the token for the next call.

    I add this string on the query and when the fields inside $url are empty I quit my execution

    <?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 (FacebookExceptionsFacebookResponseException $e) {
            echo 'm2Graph returned an error: ' . $e->getMessage();
            exit;
        } catch (FacebookExceptionsFacebookSDKException $e) {
            echo 'm2Facebook SDK returned an error: ' . $e->getMessage();
            exit;
        }
    }
    

  2. This might be caused by a server side timeout. I get this error every now and then when I request a huge amount of data. Maybe you should try to limit your request by using the limit parameter (default should be 25).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search