5

I am using the PHP library for the Graph API (http://github.com/facebook/php-sdk) but am a bit confused about how it is all working (or not).

I just want to authenticate the user and get the user ID back. What I really want to know is what to do once the user has logged in to Facebook and returned to my site. There is data called 'session' in the URL. Does this need to be stored in order to continually get the user ID? It is really not apparent, to me, from the samples or (lack of) documentation.

Also, would it be easier to just cut out the PHP library altogether and just handle the reponse myself, storing it in a session variable. If i were to do this, what is the best method of getting/extracting the current users ID?

Edit:

Currently, i have copied the facebook.php file and the example.php file over from GitHub, and only changed the application name and secret in example.php. It is not storing a cookie and is saying 'You are not connected'. However, when I print_r($session); it works (but only if the url contains the session data).

Has anyone else experienced problems like this? Is it possible that running on localhost is causing this?

Edit:

I uploaded exactly the same two files to a host and it worked perfectly. It stored the cookie and showed all information it should. It is most likely that running on localhost is causing the problems. I have changed the settings under the Connect tab on the Facebook Developer application but still no luck.

Anyone know how to do this from localhost or what i am doing wrong?

5 Answers 5

4

There is data called 'session' in the URL. Does this need to be stored in order to continually get the user ID?

Yes, it needs to be stored, but the Facebook class parses it and stores it in a cookie for you.


Also, would it be easier to just cut out the PHP library altogether and just handle the reponse myself, storing it in a session variable.

The Facebook class provided in facebook.php is not necessary to use the API but provides a few conveniences for you, such as putting together common URLs, parsing session data (which is sent to the URL and then stored in a cookie), turning error messages (a JSON object with an "error" key) into a PHP Exception, etc.

So, in short, no, it wouldn't be easier to cut out the PHP library because you'd end up writing basically the same stuff.

As you can see from the example, they are getting the User ID ($uid) by calling getUser:

if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}

$uid is just an integer, and $me is the data for the current user (which it sounds like you don't need). The same data could be retrieved by calling:

$me = $facebook->api('/' . $uid); //Generates https://graph.facebook.com/[uid]

The problem I have right now with the example is that the curl function inside the Facebook class (makeRequest method) is for some reason returning NO content. So, $me is ending up with nothing, which makes the example still say "You are not connected". But, if you were to output $uid, you would see that it does indeed contain the current user's ID.

Sign up to request clarification or add additional context in comments.

11 Comments

Does it store it in a cookie by itself or do i need to specifically run a function to do this? I have set cookie to true when declaring the facebook class but on inspecting the cookies in Firefox it doesn't appear to have stored anything.
@briggins5 It looks like it does it when you call getSession.
I have called <code>$facebook->getLoginUrl()</code> and <code>getSession</code> but still no cookies are stored, apart from some under the Facebook category, but these don't appear related specifically, and probably wouldn't be anyway.
@briggins5 Using the example as it was given, I have a cookie named "fbs_118210643380" stored after login. Where are you looking for the cookies?
I am looking at the cookie list in Firefox (the one you get from going to tools>options>privacy). I am looking under localhost but no cookies are there. Perhaps it is because it is running on the local machine and not a publicly accessible one? Also, are you using the latest GitHub version of the PHP library?
|
1

One thing that helps is checking the response headers from Facebook sometimes ( rarely ) they can be informative.

I have found that you need to make a call to /oauth/authorize (with query parameters ) to authorize the website/application, so that Facebook will give you an access token.

1 Comment

It appears that there is a problem when using localhost instead of a publicly accessible server. As far as i can tell the PHP library handles the authorisation itself as it does indeed produce the user ID, but fails to produce a cookie.
0

Have you checked out the latest version of facebook.php from github? The latest version takes account for magic quotes and thus will return a session instead of a null.

1 Comment

Thanks Peter, yes, I have that version. Maybe update anyway. It looks like just the CURL call is returning nothing (at all).
0

private function setCookieFromSession($session=null)

is failing because the $domain parameter is emtpy. I am using this for local testing:

if ($domain != '') {
    setcookie($cookieName, $value, $expires, '/', '.' . $domain);
} else {
    setcookie($cookieName, $value, $expires, '/');
}

2 Comments

Thankyou for the answer. This solved the problem of working on localhost.
so, what should the value of $domain be set to when working on localhost?
0

I think you can use this http://3aroundweb.in/facebook-connect-using-graph-api/ It contains practical guide and working code to download :)

1 Comment

This question is over a year old, it has been answered and this link is dead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.