使用FQL显示随机性别(性别)朋友的个人资料图片

时间:2012-08-30 18:50:13

标签: facebook-graph-api facebook-fql

if($user_gender == "male")
{
$fql_query_result = file_get_contents("https://graph.facebook.com/fql?q=SELECT+uid%2C+name+FROM+user+where+uid+IN+(SELECT%20uid2%20FROM%20friend%20WHERE%20uid1%20=%20me())%20and%20sex='female'ORDER%20BY%20rand%20()%20LIMIT%201&access_token=".$access_token);
}
else
{
$fql_query_result = file_get_contents("https://graph.facebook.com/fql?q=SELECT+uid%2C+name+FROM+user+where+uid+IN+(SELECT%20uid2%20FROM%20friend%20WHERE%20uid1%20=%20me())%20and%20sex='male'ORDER%20BY%20rand%20()%20LIMIT%201&access_token=".$access_token);
}

在Graph Api Explorer中显示

{
  "data": [
    {
      "uid": 100001242402868, 
      "name": "Don Omer"
    }
  ]
}

如何从上面的数据中提取UID,以便我们可以显示他/她的个人资料图片

$friend_pic = imagecreatefromjpeg("http://graph.facebook.com/".$fql_query_result['uid']."/picture?type=normal");

我正在尝试创建一个名为“谁会嫁给我”的应用。

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以在一个Query(以及一个API调用)中执行此操作。试试这个:

$query = 'SELECT uid, name, sex, pic FROM user WHERE 
            uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) 
            AND NOT ( sex IN (SELECT sex FROM user WHERE uid = me())) 
          ORDER BY rand () LIMIT 1';

快速而肮脏的方式是:

$url = "https://graph.facebook.com/fql?q=" . urlencode($query) . 
         "&access_token=" . $access_token;
$spouse = json_decode( file_get_contents ($url));
printf('<p>You should marry %s. <img src="%s" /></p>', 
       $spouse->data[0]->name, 
       $spouse->data[0]->pic
       );

但是你应该使用PHP SDK,因为它更强大。