问题
This is kinda an extension of my previous question. With that somewhat resolved, I'm still encountering errors. I'm working off of the Javascript guide here. I'm now on the last code segment of the page, towards the bottom. At the top of my page, I have the following code (APP ID and SECRET ID are defined in my code):
<?php
define('YOUR_APP_ID', '**APP ID**');
define('YOUR_APP_SECRET', '**SECRET ID**');
function get_facebook_cookie($app_id, $app_secret) {
$args = array();
parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value) {
if ($key != 'sig') {
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $app_secret) != $args['sig']) {
return null;
}
return $args;
}
$cookie = get_facebook_cookie(YOUR_APP_ID, YOUR_APP_SECRET);
$user = json_decode(file_get_contents(
'https://graph.facebook.com/me?access_token=' .
$cookie['access_token']));
?>
But now I get these errors (googling them hasn't turned anything useful up):
Again, my app id IS shown in the error code correctly.
Notice: Undefined index: fbs_**MY APP ID** in C:\wamp\www\_header.php on line 10
Notice: Undefined index: sig in C:\wamp\www\_header.php on line 18
Is it possible it could be a problem associated with WAMP? Thanks in advance.
回答1:
Check your brackets. A lot of times simple confusion among bracket hierarchies can fix these obnoxious errors.
回答2:
It means that $COOKIE['fbs'.$app_id] does not exists, it means that COOKIE var is empty (or not set)
回答3:
I've added a simple check to check for the var in the cookie. This works for me.
function get_facebook_cookie($app_id, $application_secret)
{
$args = array();
if (!isset($_COOKIE['fbs_' . $app_id])) {
//BOOHOO, NO COOKIE;
return $args;
}
parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value) {
if ($key != 'sig') {
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $application_secret) != $args['sig']) {
return null;
}
return $args;
}
回答4:
Why won't you use the Facebook PHP SDK? I've made it like this, it works for me although it don't check for the app secret:
// Read the cookie created by the JS API
$cookie = trim($_COOKIE['fbs_' . FB_APP_ID], '\\"');
parse_str($cookie, $data);
// Startup the Facebook object
$fb = new Facebook(array(
'appId' => FB_APP_ID,
'secret' => FB_APP_SECRET
));
// Say we are using the token from the JS
$fb->setAccessToken($data['access_token']);
// It should work now
var_dump($fb->getUser());
Note that the $COOKIE['fbs' . FB_APP_ID] variable will only be available after you call the FB.login method on JavaScript.
Also make sure you allow the cookie to be read from the PHP when initializing the JS API, like this:
FB.init({
appId : 'YOUR APP ID',
cookie : true
});
Hope it helped.
Link to PHP SDK's github: https://github.com/facebook/php-sdk/
来源:https://stackoverflow.com/questions/6373724/further-facebook-login-javascript-errors