0
votes

Im looking to retrieve all of my friends hometowns, I am using similar PHP as that used to retrieve their names. I think the code im using should work but I think because some users havent set a hometown it wont work and says the for each argument is invalid (although this could be completely wrong).

When simply typing in the URL to the web browser I get the following structure:

{
   "id": "z",
   "friends": {
      "data": [
        {
            "name": "x",
            "hometown": {
               "id": "x",
               "name": "x"
            },
            "id": "x"
         },

Which is then repeated for every user in the friend list, although for friends without a hometown the following is omitted:

"hometown": {
             "id": "x",
             "name": "x"
                },

To retrieve the name and birthday I have used the following PHP:


$response2 = curl_exec($ch2);
$user2 = json_decode($response2, true);
$user3=$user2{'friends'};
$user4=$user3['data'];
echo ("<h2>Friend List</h2><br>");
foreach($user4 as $friend) {
    echo $friend["name"] . "<br />" . $friend["birthday"] . "<br /><br />";
    }
}

PS I know the variable names arent particularly good but I will change them once ive figured out what im doing for the hometown!

Thanks a lot for any help in advance.

1

1 Answers

0
votes

Not everyone makes their hometown or location available to their friends. In that case you won't be able to get the data over the API and need to check for that.

To get a bit more data than just querying for the hometown, we also read out the current_location.

Through the Graph API you would use this resource: https://graph.facebook.com/me/friends?fields=hometown,location

If you are using FQL you can use a query like this:

SELECT uid, first_name, last_name, current_location, hometown_location, friend_count, mutual_friend_count FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())

Remember that you need the friends_location and friends_hometown permission to read these fields.