简体   繁体   中英

cant initialize php facebook sdk

I'm trying to do a very simple task. Initialize the facebook php sdk and return my user id. I'm wondering if there is a problem with the require_once path I have included to access the sdk, but I'm not sure. My echo message works, but nothing else. I have tried writing the require_once path a few different ways to see if it was the problem, but nothing worked.

<?php


echo "hello world";

require_once(dirname(__FILE__).'/php-sdk/facebook.php');

$APPLICATION_ID = '396***8';
$APPLICATION_SECRET = '134559c****eb4f2da';

$config = array();
$config['appId'] = $APPLICATION_ID;
$config['secret'] = $APPLICATION_SECRET;

$facebook = new Facebook(config);

$uid = $facebook->getUser();

echo $uid;






?>

The config array you pass to the constructor is wrong, it's missing the $ sign. That will give your parsing error.

$facebook = new Facebook(config); // HERE, missing $ sign for config array

If that's just a typo here, follow below.

You could try adding a die() statement after the require_once statement so to be sure it is that causing the problem. This will at least give you certainty of where the problem is.

require_once('<filename>') or die('Error is here!');

You can also check your PHP error log for any helpful error messages.

Assuming that is the entire PHP code on your page, it is working as intended, getUser() should return 0 as there is no logic that authorizes a user. You need a login handling logic for the user with either the JS SDK or getLoginUrl() function see the example https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php .

Also check to ensure you are running the latest SDK (3.2.2) https://github.com/facebook/facebook-php-sdk/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM