0

I have a problem with this piece of code, I search everywhere but didn't get answer for this problem. It's my code

 <?php

      require_once('facebook.php');


      $facebook = new Facebook(array(
        'appId'  => 'XXXXXXXXXXXXXXX',
        'secret' => 'XXXXXXXXXXXXXXX',
        ));

      $user_id = $facebook->getUser();

        echo $user_id;
    ?>

This code everytime returns 0 even when I'm logged in on facebook with my account, can someone help me to fix this problem or explain what's wrong? I'm using facebook-php-sdk-v3.1.1-21


I want to make something like this, in registration form want to add facebook like button, when user click on this button he become fan of my facebook fan page, if he don't hit like button script must check for this and output error, that before user can register must hit like button, can someone give me some tips or piece of script how to realize this with PHP?

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
  • 1
    It's not enough to be logged into Facebook. Your app needs to be too. – Ayman Safadi Jan 17 '12 at 21:02
  • 1
    There are [tons of tutorials](https://www.google.com/search?q=php+facebook+connect) out there. Take a look at some of them and give it a shot. If you have any questions, don't hesitate in coming back with a specific question. – Ayman Safadi Jan 17 '12 at 21:16
  • I want to make something like this, in registration form want to add facebook like button, when user click on this button he become fan of my facebook fan page, if he don't hit like button script must check for this and output error, that before user can register must hit like button, can someone give me some tips or piece of script how to realize this with PHP? – რეზი კაჭარავა Jan 18 '12 at 12:18

4 Answers4

0

I have the same code as github and still getuser returns 0. Here's my code: This is where I test the app, before making it viral.

<?
require 'facebook.php';
$app_id = 'xxxxxxxxxxxxxxx';
$app_secret = 'xxxxxxxxxxxxxxxxxx';

$post_login_url = "http://myhandyapps.com/tester2.php";
 $code = $_REQUEST["code"];

       //Obtain the access_token with publish_stream permission
      if(empty($code))
         {
           $dialog_url= "https://www.facebook.com/dialog/oauth?"
           . "client_id=" . $app_id
           . "&redirect_uri=" . urlencode($post_login_url)
           . "&scope=publish_stream, photo_upload, user_photos, user_photo_video_tags";
           echo("<script>top.location.href='" . $dialog_url .
           "'</script>");
       }

       else{
        $facebook = new Facebook(array(
          'appId'  => $app_id,
          'secret' => $app_secret,
          'cookie' => true,

        ));

        // Get User ID
        $user = $facebook->getUser();
        echo "$user";
        if ($user) {
          try {
            // Proceed knowing you have a logged in user who's authenticated.
            $user_profile = $facebook->api('/me');
          } catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
          }
        }

        if ($user) {
          $logoutUrl = $facebook->getLogoutUrl();
        } else {
          $loginUrl = $facebook->getLoginUrl();
        }
        //echo "$user";
        }
?>

edit: I fixed it by changing this code of mine

$post_login_url = "http://myhandyapps.com/tester2.php";

to

$post_login_url = "http://apps.facebook.com/nspace_tester";  //got this to my app (CANVAS PAGE)
VDP
  • 6,340
  • 4
  • 31
  • 53
0

I had same issue, After long time of trying to figure out what the issue is, I found that you need access token to get user ID. To get access token form the signed_request, user must authorize (to get permissions) you app firstly. I hope this will help

0

There's a good example of how to use the PHP SDK here:

https://github.com/facebook/php-sdk/tree/master/examples

<?php
/**
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

require '../src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId' => 'xxxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxx',
));

// Get User ID
$user = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}

// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');

?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>php-sdk</h1>

<?php if ($user): ?>
<a href="<?php echo $logoutUrl; ?>">Logout</a>
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
<a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
</div>
<?php endif ?>

<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>

<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>

<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>
</body>
</html>
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
DMCS
  • 31,720
  • 14
  • 71
  • 104
  • I want to make something like this, in registration form want to add facebook like button, when user click on this button he become fan of my facebook fan page, if he don't hit like button script must check for this and output error, that before user can register must hit like button, can someone give me some tips or piece of script how to realize this with PHP? – რეზი კაჭარავა Jan 18 '12 at 12:18
  • You should not ask TWO questions in one question. I've answered your original question. Please do not ask a different question inline. Please see: http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – DMCS Jan 18 '12 at 14:04
0

Seems like $facebook->getUser(); isn't working. Double check if you linked correctly to facebook.php.

Also, you might want to hide your appId and secret. :-)

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
  • Hello, This is a possible comment .. since its not actually answering question or isnt telling whats going wrong in question – NullPoiиteя Feb 08 '13 at 17:48