如何使用facebook api获取好友列表中的位置和电子邮件

时间:2011-01-06 12:40:36

标签: php facebook

请参阅以下PHP代码:

<?php
require '../src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'xxxxxxxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxx',
  'cookie' => true, // enable optional cookie support
));

try {
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
  error_log($e);
}

if ($facebook->getSession()) {     
    $friendsLists = $facebook->api('/me/friends?fields=id,name,picture,locale,email');
    $session = $facebook->getSession();

$query = "SELECT uid, name,locale,email,hometown_location FROM user WHERE uid = me() OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me())";

$params = array(  

    'access_token' => $session['access_token'], 
    'secret' => $session['secret'],
    'sig' => $session['sig'],
    'query' =>$query  

  ); 



  $url = "https://api.facebook.com/method/fql.query?" . http_build_query($params);  
  $data = simplexml_load_file($url);  

//echo $me;

pr($data);
pr($friendsLists);

  echo '<a href="' . $facebook->getLogoutUrl() . '">Logout</a>';
} else {
  echo '<a href="' . $facebook->getLoginUrl() . '">Login</a>';
}

function pr($atr){
    echo "<pre>";
    print_r($atr);
    echo "</pre>";
}
?>

我将如下所示:

[user] => Array
(
    [0] => SimpleXMLElement Object
        (
            [uid] => xxxxxxxxxxxxx
            [name] => Deepak Rathi
            [locale] => en_US
            [email] => SimpleXMLElement Object
                (
                )

            [hometown_location] => SimpleXMLElement Object
                (
                )

        )

    [1] => SimpleXMLElement Object
        (
            [uid] => xxxxxxxxxxxx
            [name] => Aakriti Rovin Ranu
            [locale] => en_GB
            [email] => SimpleXMLElement Object
                (
                )

            [hometown_location] => SimpleXMLElement Object
                (
                )

        )
)

我的问题是'email'和'hometown_locations'在这里是空的。我需要在friendlist中的两个信息。那里需要做什么..?请给予任何支持。

此致 迪帕克

1 个答案:

答案 0 :(得分:2)

Facebook返回的数据是json编码,而不是XML,所以我建议你调整你的代码。

关于您的问题 - 这些字段不为空,它们是SimpleXMLElement对象。阅读此http://php.net/manual/en/class.simplexmlelement.php以了解下一步该做什么。