thedp
thedp

Reputation: 8508

Facebook: Returns "500 (Internal Server Error)" for friends info request

I've setup the following permissions, both on my side and app's permissions' page:
(notice the "read_stream")

<fb:login-button autologoutlink="true" perms="read_stream, user_about_me, user_likes, user_location, user_birthday, user_education_history, user_work_history, friends_about_me, friends_likes, friends_location, friends_birthday, friends_education_history, friends_work_history"></fb:login-button>


Requesting for the user's profile works great:

FB.api('/me/friends', {
            fields: "id,username,name,first_name,last_name,location,link,education,work,statuses.limit(1).fields(message)"
        }, function(res){
            console.log('FRIENDS', res.data);
        });


However, getting the same for the user's friends doesn't work.
It gives a "500 (Internal Server Error)", with facebook object returning "unknown error".

FB.api('/me/friends', {
            fields: "id,username,name,first_name,last_name,location,link,education,work,statuses.limit(1).fields(message)"
        }, function(res){
            console.log('FRIENDS', res.data);
        });



I've traced the issue to the field I request. The part statuses.limit(1).fields(message) seems to be the source of the error, when removed the /me/friends returns data.
But I don't understand the reason for the error.

  1. It works for /me.
  2. According to FB docs read_steam should be for both user's statuses and user's friends' statuses.
  3. With the above fields the API Explorer gets me the friends statuses without any issues.

Am I doing something wrong or is it a FB Graph API bug?

Thank you.

Upvotes: 2

Views: 1067

Answers (1)

sven
sven

Reputation: 785

Here try this:

$user = $facebook->getUser();
if ($user){ 
           try {
                $queries = array(
                             array('method' => 'GET', 'relative_url' => '/'.$user),
                             array('method' => 'GET', 'relative_url' => '/'.$user.'/friends', 'name' =>  'get-friends'),
                             array('method' => 'GET','relative_url'  => '?ids={result=get-friends:$.data.*.id}'),
                            );

                            // POST your queries to the batch endpoint on the graph.
                            try{
                                $batchResponse = $facebook->api('?batch='.json_encode($queries), 'POST');
                            }catch(Exception $o){
                                    error_log($o);
                            }
                          } 
                        catch (FacebookApiException $e) {
                            error_log($e); //Checks if user is logged out and generate an exception if access token is invalid
                        }
                     }
                     else{
                            $params = array(
                                  'scope' => 'friends_birthday, friends_education_history, read_stream, read_friendlists, user_birthday,   //Requesting User Permissions through Facebook App
                                  'redirect_uri' => 'specify yours here' //User is redirected after Login
                                );

                        } 
 $user_details = json_decode($batchResponse[0]['body'], true);// User Own Info
 $user_friends = json_decode($batchResponse[2]['body'], true);// User's Firend Info

You'll get an array object.

Or You could use FQL, here :

$friends = $facebook->api(array(
                                        'method' => 'fql.query',
                                        'query' => 'SELECT name, pic_big 
                                             FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())'
                                    ));

https://developers.facebook.com/docs/reference/fql/friend/

I'm working on something similar, here: http://agbcorplegal.com/profile_filter/

Upvotes: 1

Related Questions