Thierry Martens
Thierry Martens

Reputation: 33

Reading results from PHP Facebook Graph api SDK

I am trying to get a list of all the names & id of people actually going to an event I create. Getting the list using the php graph api for facebook was the easy part and seems to work.

Code (php) the get the data:

//GET ATTENDING

    $getattending = "/" . $event_id . "/attending?fields=name,id";

    $req_events = new FacebookRequest($session, 'GET', $getattending);
    $req_response = $req_events->execute();
    $data_array = $req_response->getGraphObject()->asArray();

    $counter = array_map("count", $data_array);
    $count = $counter['data'];

    echo "Attending: $count<BR>";

   echo "<PRE>";
   print_r($data_array);
   echo "</PRE>";

The result:

Array (

[data] => Array
    (
        [0] => stdClass Object
            (
                [name] => Thierry Martens
                [id] => 788923242
            )

        [1] => stdClass Object
            (
                [name] => Lisa Mario Laurier
                [id] => 708876902536391
            )

        [2] => stdClass Object
            (
                [name] => Ramy Mahfoudhi
                [id] => 735036479911364
            )

        [3] => stdClass Object
            (
                [name] => Jeremy Verriest Duroisin
                [id] => 783108468420824
            )

        [4] => stdClass Object
            (
                [name] => Jonas En Svetlana Laurier
                [id] => 773139856081632
            )

        [5] => stdClass Object
            (
                [name] => Maxime Demerliere
                [id] => 849400761761008
            )

        [6] => stdClass Object
            (
                [name] => Jeremy Beauchamp
                [id] => 10204174155667109
            )

        [7] => stdClass Object
            (
                [name] => Sari Jens Delcourte Delusinne
                [id] => 10204086515874904
            )

        [8] => stdClass Object
            (
                [name] => Pieter Marysse
                [id] => 10204911283045115
            )

        [9] => stdClass Object
            (
                [name] => Patrick Vanden Bosschelle
                [id] => 10202907209181148
            )

    )

BUT i am having problems to actually gather the data itsels; i simply need the name and the id in simple array or list so i can use it in the rest of the script. Any ideas Anyone?

My second question is the php graph api seems to have a "/eventnr/attending" thing for graph 2.1; showing nr attendants to your event in question; but when i actually call it i get an error stating i need to use graph 2.1 while i uploaded the latest php sdk and can't seem to find a way to change that version. This question is not as important as the one above; but if it works i would need less code :)

Hope you guys can help me :)

Upvotes: 0

Views: 622

Answers (3)

Thierry Martens
Thierry Martens

Reputation: 33

!!!! GOT IT !!!!

Did look some further on here and the solutions seems to be pretty easy:

for ($x = 0; $x <= $count; $x++)
        {
        $names[$x] = $data_array['data'][$x]->name;
        $ids[$x] = $data_array['data'][$x]->id;
        }

Displays:

$names array:

Array ( [0] => Thierry Martens [1] => Lisa Mario Laurier [2] => Ramy Mahfoudhi [3] => Jeremy Verriest Duroisin [4] => Jonas En Svetlana Laurier [5] => Maxime Demerliere [6] => Jeremy Beauchamp [7] => Sari Jens Delcourte Delusinne [8] => Pieter Marysse [9] => Patrick Vanden Bosschelle [10] => )

$ids array:

Array ( [0] => 788923242 [1] => 708876902536391 [2] => 735036479911364 [3] => 783108468420824 [4] => 773139856081632 [5] => 849400761761008 [6] => 10204174155667109 [7] => 10204086515874904 [8] => 10204911283045115 [9] => 10202907209181148 [10] => )

Upvotes: 1

Thierry Martens
Thierry Martens

Reputation: 33

I added your code but result is the following (empty arrays)

added code:

    foreach($data_array['data'] as $a)
            {
            $o = new $a; $names[] = $o->name; $ids[] = $o->id;
            }

    echo "FIRST ELEMENT - \$data_array['data'][0]: <BR>";
    print_r($data_array['data'][0]); echo "<BR><BR>";
    echo "\$names array: <BR>";
    print_r($names); echo "<BR><BR>";
    echo "\$ids array: <BR>";
    print_r($ids); echo "<BR><BR>";

Result of the echo & print_r:

FIRST ELEMENT - $data_array['data'][0]:

    stdClass Object ( [name] => Thierry Martens [id] => 788923242 ) 

$names array:

    Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => ) 

$ids array:

    Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => )

Upvotes: 0

StackSlave
StackSlave

Reputation: 10617

It's like:

$dataArray = $data_array['data'];
$firstPerson = new $dataArray[0];
echo $firstPerson->name;
echo $firstPerson->id;

Maybe you need this, though:

foreach($data_array['data'] as $a){
  $o = new $a; $names[] = $o->name; $ids[] = $o->id;
}
// $names is Array of names
// $ids in Array of ids

Upvotes: 0

Related Questions