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 does not connect to the Facebook API, but I do not see what happened. It seems that something with the line $facebook->api('/me').

+3
source share
1 answer

I don't see much with code. However, there are a few things that are different from 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 in general, do you use $me? Make sure you specify $logoutUrland $loginUrl.

Are any errors displayed?

Hope this helps!

+2
source

Source: https://habr.com/ru/post/1778299/


All Articles