Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the Facebook Graph APIs PHP library

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?

like image 314
j82374823749 Avatar asked Apr 22 '10 22:04

j82374823749


2 Answers

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.

like image 116
Nicole Avatar answered Sep 20 '22 17:09

Nicole


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, '/');
}
like image 35
danieljones.org Avatar answered Sep 18 '22 17:09

danieljones.org