View Full Version : Help with FB connect

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:


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);
$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"
<?php if ($cookie) { ?>
Your user ID is <?= $cookie['uid'] ?>
<?php } else { ?>
<?php } ?>

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

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

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=' .
register_user($user->id, $user->email, $user->name, $user->username,
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 :)

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 :(

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.

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

Any idea where I am wrong?

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.

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.

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...

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=' .

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=' .

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

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

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

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.

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!!!!