PDA

View Full Version : Help with FB connect



midhul
06-07-2010, 06:11 PM
Hey guys,

This is not any topic realred to the core depths of facebook connect, but simple one that any non confused php programmer can answer :)
Please refer to this doc regarding single sign on feature of fb connect: http://developers.facebook.com/docs/guides/web

actually I was building an app which uses fb login button then requests user email and send it to another form to process.

But I am abit confused about how to request the user data after the log on has been made.
Here is the code:

<?php

define('FACEBOOK_APP_ID', 'your application id');
define('FACEBOOK_SECRET', 'your application secret');

function get_facebook_cookie($app_id, $application_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 . $application_secret) != $args['sig']) {
return null;
}
return $args;
}

$cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);

?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:fb="http://www.facebook.com/2008/fbml">
<body>
<?php if ($cookie) { ?>
Your user ID is <?= $cookie['uid'] ?>
<?php } else { ?>
<fb:login-button></fb:login-button>
<?php } ?>

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({appId: '<?= FACEBOOK_APP_ID ?>', status: true,
cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
</script>
<?php


$user = json_decode(file_get_contents(
'https://graph.facebook.com/me?wrap_access_token=' .
$cookie['oauth_access_token']))->me;
register_user($user->id, $user->email, $user->name, $user->username,
$user->birthday_date);
echo $user->email;
?>
</body>
</html>

as you can see after requesting log on with the fb connect button, I have use a php code to request and echo the users email:




$user = json_decode(file_get_contents(
'https://graph.facebook.com/me?wrap_access_token=' .
$cookie['oauth_access_token']))->me;
register_user($user->id, $user->email, $user->name, $user->username,
$user->birthday_date);
echo $user->email;

I mean I used echo $user->email, but it is not working :(
I think I have used the wrong variable.

Can some please help me and tell me how to do it?

Thanks alot in advance :)

midhul
06-07-2010, 06:19 PM
Ps- please ignore the stars "*" there is a bug In The forum, i am unable to edit my post :(

djr33
06-07-2010, 10:53 PM
I replaced all of the '*'s with spaces in your post. I'm not sure why that happened, but for now at least the post is formatted correctly.

midhul
06-08-2010, 03:42 AM
Thhans alot :D

Any idea where I am wrong?

djr33
06-08-2010, 06:03 AM
I'm not exactly sure of the problem. Do you know how to use classes in PHP? $user->email is part of the class. Once defined, that acts as an internal variable within the $user object.
I don't know enough about facebook connect to be sure that's right, but I don't see anything (obvious) wrong in your code.

However, json_decode() creates arrays, not objects, right? So many you mean $user['email']??

Try <?php print_r($user); ?> to see if this outputs an array. If so, it's an array. If you get some error, it's an object like I said above.

Note: json is a function for javascript. It may work in PHP, but I've never used it, so if that doesn't work you might need to find another way. But I think this is ok-- it's just possible that this is part of the problem.

midhul
06-08-2010, 11:09 AM
Thanks for your time :)

But I tried |||| print_r($user); ||| before, it does not output anything.
Not even PHP error.

Can you see this please: http://developers.facebook.com/docs/guides/web
It has info about how the FB connect 'json' function works :D

Thanks alot.

djr33
06-08-2010, 07:37 PM
Don't they have a forum there? This isn't really a question for php, but a question for how FB connect works. I've never used it, so aside from learning everything about it, I'm not sure how to help. Maybe someone else here knows...

Ron14
06-13-2010, 02:28 AM
The example code has changed. Try the following:

$user = json_decode(file_get_contents(
'https://graph.facebook.com/me?access_token=' .
$cookie['access_token']))->id;

use echo $user to get the id.

for the email it was a bit different. I found this to work:

$user = json_decode(file_get_contents(
'https://graph.facebook.com/me?access_token=' .
$cookie['access_token']));

then echo $user->mail after adding an extended permisson for email.

<fb:login-button perms="email"></fb:login-button>

vishnuk
06-14-2010, 12:15 PM
Warning: file_get_contents(https://graph.facebook.com/me?wrap_access_token=) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in

can any one help

djr33
06-14-2010, 08:27 PM
The URL is incorrect. The error message tells you this. Look at the documentation again.
Also, this might be the right URL, but your access token is missing: add this to the end of the URL.

LilPeck
07-04-2010, 11:00 AM
I realize this issue is resolved for the OP, but if someone else comes along having problems with the new Facebook Graph API, I came up with a variation that works fine for me. I posted a demo, tutorial, and the code at facebookgeewhiz.com.

Long time dynamicdrive.com user here!!!!