user529345
user529345

Reputation: 31

Connecting to Facebook API with PHP

<?php
$user_name = "root";
$password = "root";
$database = "tvfanatic";
$server = "localhost";
$db_handle = mysql_connect($server, $user_name, $password) or die(mysql_error());
$db_found = mysql_select_db($database, $db_handle);

require 'facebook.php'; 

$facebook = new Facebook(array(
    'appId'  => 'MYAPPID',
    'secret' => 'MYAPPSECRET',
    'cookie' => true,
));
$session = $facebook->getSession();

$me = null;
// Session based API call.
if ($session) {
    try {
        $uid = $facebook->getUser();
        $me = $facebook->api('/me');
    }
    catch (FacebookApiException $e) {}

    if ($db_found) {
        echo $me['id'];
        $user_id = $me['id'];
        $username = $me['name'];
        $EXIST = mysql_query("SELECT * FROM users WHERE fb_id='$user_id'");

        if (mysql_num_rows($EXIST) == 0 ){
            $SQL = "INSERT INTO users (fb_id, name) VALUES ('$user_id','$username')";
            $result = mysql_query($SQL);
        }
    }
    else{
        mysql_close($db_handle);
    }
} ?>

My application isn't connecting to the Facebook API but I can't see what's wrong. It seems to be something with the line, $facebook->api('/me').

Upvotes: 3

Views: 577

Answers (1)

ryryan
ryryan

Reputation: 3928

I can't see a lot wrong with the code. However, there is a few things different to mine.

$facebook = new Facebook(array(
    'appId'  => '****************',
    'secret' => '*************************',
    'cookie' => true,
));

$session = $facebook->getSession();

$me = null;
// Session based API call.
if ($session) {
    try {
        $uid = $facebook->getUser();
        $me = $facebook->api('/me');
    }
    catch (FacebookApiException $e) {
        error_log($e); //you haven't got that
    }
}

And are you using the $me at all? Make sure you've declared the $logoutUrl and $loginUrl.

Are there any errors displaying?

Hope it helped!

Upvotes: 2

Related Questions