0

The following code should return the email address of the user logged in.

  <?php


     require_once('facebook-php-sdk/src/facebook.php');
    // Create our Application instance (replace this with your appId and secret).
    $facebook= new Facebook(array(
      'appId' => '2453463636',
          'secret' => '365653656565656',
          'allowSignedRequest' => false,
    ));


    $user = $facebook->getUser();
    print_r($user);
    if ($user) {
      try {
        // Get the user profile data you have permission to view
       // $user_profile = $facebook->api('/me');
        $user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
        echo $user_profile['email'];

      } catch (FacebookApiException $e) {
        $user = null;
 error_log($e);
     print_r($e);
      }
    } else {
     die("failed");
    }

    ?>

Instead it returns "failed", and print_r($user) returns 0.

Really don't know what to do.

In the dashboard:

enter image description here

I have set up a website platform to view my app.

The jist: The "App Domains" = www.app.com

"Site Url" = http://www.app.com/test

The site url is actual the app. I try to access the getUser in urls such as http://www.app.com/test/fbuser, where it returns 0 and die clause.

Did I set this up wrong?

EDIT

Curl on my localhost server

enter image description here

also the "Configure Command" is quite different(see below for localhost screen shot)

enter image description here

The remote server info mentions "curl" in this blob of text for Configure Command

Tester
  • 2,887
  • 10
  • 30
  • 60
  • What happens if you use the example from FB: https://developers.facebook.com/docs/php/howto/profilewithgraphapi/ – Tobi Jan 22 '14 at 20:58
  • I tried this. It brings up the link to login, even though I am logged in, and when I click it this is appended to the end of url in the address bar "?code=AQDJgFwlShcurgvYqmPfzlZANlAnilY8DxwYA5YS-Oh_tGURhgQO9vI5HHbEiLZJ7B0GC4VgO3zkslvwmVwmOglL-o1pZteNbb4a39VbdcTu6inLwD8sTHG3CFapzB-fYsPMTJfNv-MiAuAaYASo0z-TiIyYuzAaNqZsjSoH5GPoqn6ynZdXAyCCkyN9SqZv8Br9Aj73KLwFzSw3SNHzKAl5k75BvUwDUtDvKQc3V76L4oHk2s-LlCsBL_4_QBibvVvIIeGokvB473oiR002346UxDYe-1cPvH2sAn_hHbk2B4GfRWijpMF8R2E744h7VfE&state=e17f5d8c1cfb47b357df6d5ff1eecca6#_=_", but the page stays the same – Tester Jan 22 '14 at 21:03
  • @Tobi Please see edits above – Tester Jan 22 '14 at 21:10
  • This is not connected to whether you're logged at www.facebook.com It's about whether you already gave permission to the app to access you info. You'll need to implement the OAuth auth flow as described here: https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/#login The "?code=" portion can be exchanged to an access token likek this: https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/#confirm – Tobi Jan 22 '14 at 21:10
  • Are you using the latest version of the FB PHP SDK? – madebydavid Jan 23 '14 at 01:06
  • @madebydavid I think so. Downloaded it from https://github.com/facebook/facebook-php-sdk – Tester Jan 23 '14 at 01:14
  • @madebydavid This same exact code worked with localhost, Only differnec was that the facebook app i created for local testing had a App DOmain set to localhost, and Site Url set to http://localhost:8080/fbuser. Don't knwo why it's not working now when pushed to remote server – Tester Jan 23 '14 at 01:17
  • Does the remote server have curl installed? The SDK needs curl to swap the code for an access token. Can you create a test.php file with ` – madebydavid Jan 23 '14 at 01:29
  • Actually, it should die before that point if curl was not included – madebydavid Jan 23 '14 at 01:31
  • @madebydavid See http://gowrie.host56.com/phpInfo.php, but the CURL info here is very different from what is on my localhost (see above image for localhost screen grab) – Tester Jan 23 '14 at 01:38
  • Very strange! Sorry for all the questions - it is hard to narrow down the problem. Is the app sandboxed and you're testing with a user who is not listed as a developer of the app? – madebydavid Jan 23 '14 at 01:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45849/discussion-between-tester-and-madebydavid) – Tester Jan 23 '14 at 02:04

2 Answers2

3

Codeigniter deletes $_REQUEST which is used by the Facebook PHP SDK to retrieve the OAuth code param.

The fix is to copy $_GET to $_REQUEST before your call to $facebook->getUser():

$_REQUEST += $_GET; 
$user = $facebook->getUser();

Thanks to this SO answer.

Community
  • 1
  • 1
madebydavid
  • 6,457
  • 2
  • 21
  • 29
0

Case 1 If User is logged in and still getting $user = 0

I faced this issue on different occasions but SDK issue is not happening to everyone. So I'm not sure what goes wrong here. But I dig in to it little bit and found solution as mentioned below.

SOLUTION : This worked for me after trying for many solutions for this issue.

In base_facebook.php file, find the makeRequest() method and check for following Line.

$opts = self::$CURL_OPTS; 

Immediately following it, add this line

$opts[CURLOPT_SSL_VERIFYPEER] = false;  

More details can be found here - http://net.tutsplus.com/tutorials/php/how-to-authenticate-your-users-with-facebook-connect/

Case 2 - If user is not Logged in

Try following

if($user_id) {
  // User is logged in do action here

} else {

  // No user, print a link for the user to login
  $login_url = $facebook->getLoginUrl();
  echo 'Please <a href="' . $login_url . '">login.</a>';

}
Community
  • 1
  • 1
  • Are you able to get user id in $user or you are still getting $user = 0 ? – Trimantra Software Solution Jan 22 '14 at 19:23
  • The code is exactly as above. I tried printing $user, but it always returns 0. I really don't understand, this was working fine on localhost xampp – Tester Jan 22 '14 at 19:28
  • Edited my answer for possible options – Trimantra Software Solution Jan 22 '14 at 19:38
  • Tried the second option. I logged out and reloaded the page, which brought up the link which took me to the login page. I logged in, and then the app asked to access my basic info, I clicked ok, and it redirected to the page with the same value 0 from the print_r and the login link. Exept now when I clcik the login link it loads a strange url, put stays on the page. – Tester Jan 22 '14 at 19:50
  • Seems like you got logged in properly but now you got problem with userid returning 0. I would suggest you to try option 1 again and refresh page. – Trimantra Software Solution Jan 22 '14 at 19:59
  • Tried again and again. The same result on the page. Could this possible be a server config problem. – Tester Jan 22 '14 at 20:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45835/discussion-between-tester-and-trimantra-software-solution) – Tester Jan 22 '14 at 20:05