views:

29

answers:

3

I'm a novice when it comes to java and not awesome at php, so dont judge me if this is realy easy or impossible...

I would like to extract the users facebook-id and put it into a php-variable. How do i do that?

The code im using is from a facebook tutorial video, and its working. I just want to be able to use the user id in a php code.

This is my code:

<form action="" method="post">
<div id="user">
 Name: <input name="name" size="27" /><br />
    <fb:login-button length="long" onlogin="update_user_box();"></fb:login-button>

</div>
<textarea name="comment" cols="30" rows="5"></textarea><br />
<input type="submit" value="Submit comment" />
</form>


<script type="text/javascript" src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php"&gt;&lt;/script&gt;

<script type="text/javascript">

function update_user_box() {
 var user_box = document.getElementById("user");
 user_box.innerHTML =
 "<span>"
 +"<fb:profile-pic uid='loggedinuser'></fb:profile-pic>"
 +"Welcome, <fb:name uid='loggedinuser' useyou='false'></fb:name>. "
 +"You are signed in with your Facebook account."
 +"<fb:uid uid='loggedinuser'></fb:uid>"
 +"<a href='#' onclick='FB.Connect.logout(function() { reload(); }); return false;' ><img id='fb_logout_image' src='http://static.ak.fbcdn.net/images/fbconnect/logout-buttons/logout_small.gif' border='0' alt='Connect'/></a>"
 +"</span>";

 FB.XFBML.Host.parseDomTree();
}

FB.init("API-KEY","xd_receiver.htm");

FB.ensureInit ( function () {
        FB.Connect.ifUserConnected(update_user_box);
});
</script>
A: 

You can't pass a variable from JavaScript to PHP; JavaScript is a client-side language (ie. it's executed by the browser), whereas PHP is a server-side language (ie. it's executed by the server).

It's possible to pass data from JavaScript to PHP using an XMLHttpRequest (commonly known as AJAX) to send a HTTP request to a PHP script, but I'm not sure that's what you want.

Johannes Gorset
A: 

Once the user is logged in (usually using javascript FB.login()), a cookie is set. PHP can access that cookie. Check out the following PHP code (from http://developers.facebook.com/docs/guides/web#login):

<?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);

?>

You can then get $cookie('uid'), and use that where you need it.

The page I linked above should have all you need to know about using the JavaScript SDK in conjunction with PHP.

Ryan Kinal
A: 

You should include the facebook client library for php and then you can get the user id easily something like this:

include ('facebook_client_library.php');
$facebook = new facebook('API', 'SECRET');
$fbid = $facebook->get_loggedin_user();
echo $fbid;

Or try this javascript (not sure):

alert(FB.Facebook.apiClient.get_session().uid);
Sarfraz