如何使用facebook api和fql获取朋友性别

时间:2012-07-08 16:59:37

标签: php facebook facebook-graph-api facebook-fql

我是PHP和FQL的新手。我知道它需要怎么做,但不知道代码。创建一个应用程序,用户只能在框中看到相反的性别。

我起身得到了用户的姓名,性别和朋友ID。现在我需要帮助运行FQL并获取所有朋友的性别并插入异性的名称。

<?php  
    $fbuser = $facebook->api('/me');//getting users data
    $friends = $facebook->api('/me/friends'); //getting friends data (only gives id and name)
    $userName = $fbuser['name'];//getting users name
    $userGender =  $fbuser['gender'];//getting users gender

    $friendsId = array();//empyt array to insert friends id
    foreach ($friends['data'] as $value);{ //loop
        array_push($friendsId, $value['id']); // not sure if this correct

        $friendsGender = friendsGender ;//run FQL to get the friends gender using the id

        //conditional Statement, if users and friends gender is not same insert the friends name in <select>

?> 

我做了以下但是没有用。我的第二行脚本是否正确?

$friendsInfo = $facebook->api('/me/friends', array('fields' => 'id,name,gender'));
$friendsGender = $friendsInfo['gender'];
echo $friendsGender;

2 个答案:

答案 0 :(得分:3)

您可以通过以下方式获取朋友的数据:

foreach ($friends['data'] as $value){
    $friend = $facebook->api('/'+$value['id']);

但那效率很低。幸运的是还有另一种解决方案 - 您可以指定您希望Facebook返回的字段:

$facebook->api('/me/friends', array('fields' => 'id,name,gender'));

PS。它是图形api调用; fql是别的东西

PPS。 foreach ($friends['data'] as $value);{是错误的 - 块在循环之外/之后

答案 1 :(得分:1)

您可以尝试以下方式:

<?php

$access_token = 'AABBCC...';
$param = array(
    'access_token'  => $access_token,
    'method'        => 'GET',
    'q'             => 'SELECT name, sex FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())'
);

$fql = 'https://graph.facebook.com/fql?' . http_build_query($param);


$res = file_get_contents($fql);
$aRes = json_decode($res, 1); 

您甚至可以选择过滤添加WHERE sex = "female" AND uid IN (..)到您的查询