views:

4427

answers:

4

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?

+2  A: 

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.

Renesis
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`.
Renesis
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?
Renesis
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?
@briggins5 It's possible that you aren't actually getting logged in. I'm using a local machine but it has an external IP address so maybe that makes a difference. I'm using a version from GitHub from last night. I also got the curl function problem solved.
Renesis
OK. Before i upload to a host, can i just have some details about what things you are doing? As i said earlier, i am doing the following: 1) call getLoginUrl, 2) call getSession on return. When the session information is in the URL i can get the user ID, however when i return to the same page (in other words, remove the URL variable) it doesn't work. It seems it is just getting a URL variable and not a cookie. Thanks for the help by the way :)
@briggins5 I am just using example.php. Sounds like you may have some problems with storing cookies that may not be related to the Facebook API.
Renesis
If you leave the page, then go back to it you are still logged in? I seem to be able to store cookies OK as i just tested it via <code> setCookie </code>. Tommorow i will go through the library and try sticking it in a session instead, as well as some more tests to make sure.
After changing some settings, and a lot of testing, i have reached the same point as you. Where echoing <code>$facebook->getUser()</code> gets the user ID, but using <code>$me</code> doesn't. The cookie is still not beign created though. However, it does work fine when using a web host.
The problem I had with the curl function in facebook.php was the absence of `curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);`. I added that and it worked fine after that.
Renesis
A: 

Hi Renesis, 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.

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

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.

Nick Rich
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.
+1  A: 

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, '/');
}
danieljones.org
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?
Evans