1

How do I get a user's top 10 friends' uids, and wrap each uid with specified string?

From: Get list of top friends for facebook app, I see this:

$statuses = $facebook->api('/me/statuses');

foreach($statuses['data'] as $status){
// processing likes array for calculating fanbase. 

        foreach($status['likes']['data'] as $likesData){
            $frid = $likesData['id']; 
            $frname = $likesData['name']; 
            $friendArray[$frid] = $frname;
        }

 foreach($status['comments']['data'] as $comArray){
 // processing comments array for calculating fanbase
            $frid = $comArray['from']['id'];
            $frname = $comArray['from']['name'];

}

But what does that return? Does it return the user IDs of friends in an array? I would like to get it in an array, the result of the search, so I can wrap each ID using foreach and do what I please with it.

If the above code is enough, should I be calling $frid for the array of top friends? I just need comprehension. :o)

Thank you for your time.


Assume that permissions are granted.
(This happens only after the user allows permission, so assume we already have that.)

Community
  • 1
  • 1
Mafia
  • 792
  • 2
  • 19
  • 39
  • 1
    Examine the return with `var_dump($statuses['data'])` or just `var_dump($statuses)`. – Michael Berkowski Jun 09 '12 at 15:40
  • 1
    If you’re not sure about the return type of anything in PHP, please use var_dump, it’ll tell you. – CBroe Jun 09 '12 at 15:41
  • Thank you guys, it returned `array(0) { } ` ... So, what to do? :) – Mafia Jun 09 '12 at 15:56
  • **var_dump($statuses)** returns `array(1) { ["data"]=> array(0) { } } ` while **var_dump($statuses['data'])** returns `array(0) { }` – Mafia Jun 09 '12 at 16:06
  • 1
    It sounds like you don't have a valid access token. See the [Authentication section](https://developers.facebook.com/docs/authentication/) on the developer site. [Authenticating as an app](https://developers.facebook.com/docs/authentication/applications/) is the easiest way if you're new at this. – cpilko Jun 09 '12 at 17:20

1 Answers1

5

Ensure you have the latest SDK by going to https://github.com/facebook/facebook-php-sdk/zipball/master

Unzip and you have a layout as shown below

/facebook-php-sdk
index.php

Where index.php with the file being used to display in the browser the number of friends

Include the SDK at the start of the PHP file

require('sdk/src/facebook.php');

Go to https://developers.facebook.com/apps, select your app and get your App ID and App Secret, create an instance within the PHP file

$facebook = new Facebook(array(
  'appId'  => 'YOUR_APP_ID_HERE',
  'secret' => 'YOUR_SECRET_HERE',
));

Then retrieve the $user data so we know that the current user is authenticated

$user = $facebook->getUser();

Before sending any calls check whether the authentication was right

if ($user) {
  try {
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

Now make a call to /me/statuses the documentation is available at https://developers.facebook.com/docs/reference/api/user/#statuses

$statuses = $facebook->api('/me/statuses');

This should return an array of the structure Status message defined at http://developers.facebook.com/docs/reference/api/status/ of all the current user status messages.

Now you need to decide what determines top friends

  • number of likes + comments
  • number of comments
  • number of likes

Let's choose option 1, and give each weight of 1. That is a like and a comment are equivalent in value for determining the amount of friends

So create a variable to hold this, for example $friendArray

Then make an iteration over all the status messages but the entire JSON reponse starts with a wrapped data

{
  "data": [

So access $statuses['data'], the foreach will give all status messages as a status item

foreach($statuses['data'] as $status){

Within this loop iterate all the likes and increment the value of each id that appears

foreach($status['likes']['data'] as $likesData){
    $frid = $likesData['id']; 
    $friendArray[$frid] = $friendArray[$frid] + 1;
}

Within this loop iterate all the comments and increment the value of each id that appears

foreach($status['comments']['data'] as $comArray){
    $frid = $comArray['from']['id'];
    $friendArray[$frid] = $friendArray[$frid] + 1;
}

At the end of the outer loop foreach($statuses['data'] as $status){ you should have the an array $friendArray which has the scores.

Call asort http://www.php.net/manual/en/function.asort.php to sort the array and you can then loop for the top x scores.

The code you showed isn't a function and is actually missing a closing brace, it does not actually return anything as it is not a function.

Caveats: The /me/statuses only returns a limited set of status messages per call you need to get previous page calls to iterate all your messages. The top friends returned are only based on the restriction I made up above.

phwd
  • 19,975
  • 5
  • 50
  • 78