Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Help with FB connect

  1. #1
    Join Date
    Feb 2008
    Posts
    81
    Thanks
    8
    Thanked 5 Times in 5 Posts

    Question Help with FB connect

    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
    Last edited by djr33; 06-07-2010 at 09:52 PM.

  2. #2
    Join Date
    Feb 2008
    Posts
    81
    Thanks
    8
    Thanked 5 Times in 5 Posts

    Default

    Ps- please ignore the stars "*" there is a bug In The forum, i am unable to edit my post

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. The Following User Says Thank You to djr33 For This Useful Post:

    midhul (06-08-2010)

  5. #4
    Join Date
    Feb 2008
    Posts
    81
    Thanks
    8
    Thanked 5 Times in 5 Posts

    Default

    Thhans alot

    Any idea where I am wrong?

  6. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. The Following User Says Thank You to djr33 For This Useful Post:

    midhul (06-08-2010)

  8. #6
    Join Date
    Feb 2008
    Posts
    81
    Thanks
    8
    Thanked 5 Times in 5 Posts

    Wink

    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

    Thanks alot.

  9. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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...
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #8
    Join Date
    Jun 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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>
    Last edited by Ron14; 06-13-2010 at 01:39 AM. Reason: additional info about extended permission "email"

  11. #9
    Join Date
    Jun 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  12. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •