AFD
AFD

Reputation: 1419

How to get data gender in Facebook API?

I want to show gender using this, but it's not working:

foreach($results as $val) {
  $url = 'https://graph.facebook.com/'.$val->id.'/?fields=gender';
  $data = json_decode($url,1);
  echo $val->id.' : '.$data->gender;
}

Target output:

(id : gender)
12345 : male
12346 : female
etc.

Upvotes: 1

Views: 1327

Answers (1)

Stéphane Bruckert
Stéphane Bruckert

Reputation: 22873

1. You don't get the content of the URL

file_get_contents

2. The Facebook API data is contained in a global array which key is data.

$data->gender; should be (both are ok):

  • $data->data->gender;
  • $data['data']['gender'];

foreach($results as $val) {
    $url = 'https://graph.facebook.com/'.$val->id.'/?fields=gender';
    $request = file_get_contents($request_url);
    $json = json_decode($request);
    echo $val->id.' : '.$json->data->gender;
}

Upvotes: 1

Related Questions