1
votes

I am writing an application that will print all of the user's friends.

<?php
$api_key = 'xxxxxxxxxx';
$secret  = 'xxxxxxxxxxxxxxxxx';

include_once './facebook-php-sdk-master/src/facebook.php';

$facebook = new Facebook($api_key, $secret);
$user = $facebook->getUser(); 

?>
<h1>Facebook friends</h1>
Hello <fb:name uid='<?php echo $user; ?>' useyou='false' possessive='true' />! <br>
Your id : <?php echo $user; ?>.

Friends List:<br>
<?
$friends = $facebook->api('/me/friends');

?>

<ul>
<?
foreach($friends as $friend){
    echo "<li><fb:name uid=\"$friend\" useyou=\"false\"></li>"; 
}
?>
</ul>

i am getting this as result:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/kurokmed/public_html/fb/index.php:2) in /home/kurokmed/public_html/fb/facebook-php-sdk-master/src/facebook.php on line 49 Facebook friends

Hello ! Your id : 0. Friends List:

Fatal error: Uncaught OAuthException: Error validating application. Cannot get application info due to a system error. thrown in /home/kurokmed/public_html/fb/facebook-php-sdk-master/src/base_facebook.php on line 1254

please tell me what am i doing wrong

2
Try calling the $friends = $facebook->api('/me/friends/') before echoing anything and see what happens... - Loupax
$user is 0 meaning user has not logged in also. otherwise it wud have printed user id of facebook user. - Alpesh Prajapati

2 Answers

1
votes

Functions that send/modify HTTP headers must be called before any output to the browser is made otherwise you'll get the error:

Warning: Cannot modify header information - headers already sent (output started at file:line)

This error will tell you which file is causing the problem and on what line.

The second error you've posted - check your facebook app settings.

1
votes
<?php
$api_key = 'xxxxxxxxxx';
$secret  = 'xxxxxxxxxxxxxxxxx';

include_once 'facebook-php-sdk-master/src/facebook.php';

$facebook = new Facebook($api_key, $secret);
$user = $facebook->getUser(); 

    if ($user) {
        $user_profile = $facebook->api('/me');
        $friends = $facebook->api('/me/friends');

        echo '<ul>';
        foreach ($friends["data"] as $value) {
            echo '<li>';
            echo '<div class="pic">';
            echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture"/>';
            echo '</div>';
            echo '<div class="picName">'.$value["name"].'</div>'; 
            echo '</li>';
        }
        echo '</ul>';
    }
?>