Marco Baratella
Marco Baratella

Reputation: 51

How to make a Facebook FQL query to select all friends

I'm new with PHP SDK Facebook, so sorry if that quesiton is stupid.

So, i have this code

SELECT name FROM user
    WHERE current_location.country = 'Italy' AND
    uid IN (
        SELECT uid2 FROM friend WHERE uid1 = me()
    ) 

That responses me:

{
  "data": [
    {
      "name": "Matilde xxxx"
    }, 
    {
      "name": "Samson xxxxxxxx"
    },  
    {
      "name": "Emanuela xxxxxxx"
    }
  ]
}

I'm using this site to analyze how to manage the json :http://chris.photobooks.com/json/default.htm

i saw that if i want to have a request of the name of a friend i have to do that: root.data[5].name

so that is my php code :

$user_id = $facebook->getUser();


$all_friends = $facebook->api(array('method' => 'fql.query', 'query' => 'SELECT name FROM user
        WHERE current_location.country = "Italy" AND
        uid IN (
            SELECT uid2 FROM friend WHERE uid1 = 1068943079
        ) '));
        $val = 1;
        echo $all_friends['data'][$val]['name'];

(is a test, i want to see the friend at key=1) but there is only white page... show nothing.. and if i do echo $all_friends i see Array.

Sorry if this is a stupid question, i know that i can use another method to do that..but i want to know how to do it with this way! Thank you, and sorry for my language.

That is all my code : CLICK HERE TO SEE MY CODE

Upvotes: 1

Views: 1329

Answers (1)

Stéphane Bruckert
Stéphane Bruckert

Reputation: 22893

1. You should print_r($all_friends) to check the structure of the array

Try

echo $all_friends[$val]['name'];

2. The FQL query should only occur when $user != 0.

Place it under the if statement

3. You didn't ask for the read_friendlists permission

$params = array(
  'scope' => 'read_friendlists'
);

$loginUrl = $facebook->getLoginUrl($params);

Doc:

Upvotes: 1

Related Questions