1

我是 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;
4

2 回答 2

3

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

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

但这是非常低效的。幸运的是,还有另一种解决方案 - 您可以指定希望 facebook 返回哪些字段:

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

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

聚苯乙烯。foreach ($friends['data'] as $value);{有问题 - 块在循环之外/之后

于 2012-07-08T17:45:10.000 回答
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 (..)到您的查询

于 2012-07-08T22:38:38.740 回答