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

问题描述:

Please see this below PHP code:

<?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>";
}
?>

I am getting out put as below:

[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
                (
                )

        )
)

My problem is that 'email' and 'hometown_locations' is empty here.I need both information in friendlist.What need to do there ..?Please give any support.

Regards Deepak

Data returned by Facebook is json encoded, not XML so I suggest you adjust your code.

About your question - those fields are not empty, they are SimpleXMLElement Objects. Read this http://php.net/manual/en/class.simplexmlelement.php to get to know what to do next.