views:

2250

answers:

2

I am doing my second Facebook connect site. On my first site, I used the facebook coonect FBML to sign a user in, then I could access other information via the PHP Client. With this site, using the same exact code, it doesn't seem to be working. I'd like someone to be able to sign in using the Facebook button:

<fb:login-button length="long" size="medium" onlogin="facebook_onlogin();"></fb:login-button>

After they have logged in, the facebook_onlogin() function makes some AJAX requests to a PHP page which attempts to use the Facebook PHP Client. Here's the code from that page:

 $facebook = new Facebook('xxxxx API KEY xxxxx', 'xxxxx SECRET KEY xxxxx');
 //$facebook->require_login(); ** I DONT WANT THIS LINE!!

 $userID = $facebook->api_client->users_getLoggedInUser();

 if($userID) {

  $user_details = $facebook->api_client->users_getInfo($userID, 'last_name, first_name'); 
  $firstName = $user_details[0]['first_name']; 
  $lastName = $user_details[0]['last_name']; 

  $this->view->userID = $userID;
  $this->view->firstName = $firstName;
  $this->view->lastName = $lastName;
 }

The problem is that the PHP Client thinks I don't have a session key, so the $userID never get set. If I override the $userID with my facebook ID (which I am logged in with) I get this error:

exception 'FacebookRestClientException' with message 'A session key is required for calling this method' in C:\wamp\www\mab\application\Facebook\facebookapi_php5_restlib.php:3003

If I uncomment the require_login(), it will get a session ID, but it redirects pages around a lot and breaks my AJAX calls. I am using the same code I successfully did this with on another site. Anyone have any ideas why the PHP client don't know about the session ID after a user has logged in with the connect button? Any ideas would be greatly appreciated. Thanks!

A: 

You can extract the user id without a API call, directly from the cookies, by using:

$facebook->get_loggedin_user();

Note that this is on the main $facebook object, not the api_client object it contains.

Frank Farmer
Hey, that still isn't getting the ID unless I call the $facebook->require_login(); function. Is there some other function I can call that will setup the session ID for the PHP Client?
Ryan
Or is there a way to get the User's information (first name, last name, etc) from PHP?
Ryan
Make sure that you have a valid set of Facebook cookies, which should have been created by the facebook connect JS library when you logged in using the button. If the user hasn't logged in via Connect, you won't get anything out of get_loggedin_user() [since no one is logged in]. And no, you can't get first name/last name, etc. without an API call; only the user id.
Frank Farmer
A: 

Never try to use facebook connect and the PHP client from localhost.... that's the answer. You can get it to work but it causes several 'hidden' issues that you don't have to deal with when working from a public domain.

Ryan