简体   繁体   中英

Error calling the Facebook Login

I'm new to web development, still a beginner.

I unsure on how to code for session calling the FaceBook if there any. However, I've added this at all pages which I want my facebook to be loaded. I believe it is the Facebook SDK. For example, I add this code in my register.php and login.php.

<body>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '<?php echo $config['appId']; ?>',
      xfbml      : true,
      version    : 'v2.4'
    });
  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "//connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
</script>

I also added this in my config.php which I believe it is Config for FB Login.

$config = array();

However, every time I tried to click on the Facebook button in my login.php, there will be error such that

Fatal error: Call to a member function getLoginUrl() on a non-object in C:\\wamp\\www\\sggrocer\\loginFb.php on line 13

loginFB.php

<?php   
    session_start();
    include('ajax-helper/config.php');

    $_SESSION[$PROJECT_NAME . '-loginFb'] = true;
    unset($_SESSION[$PROJECT_NAME . '-logout']);

    $params = array(
      'scope' => 'email, user_birthday',
      'redirect_uri' => $base_url . "http://localhost/sggrocer/",
    );

    $loginUrl = $facebook->getLoginUrl($params);

    header('Location:' . $loginUrl);

?>

This is my login.php: https://jsfiddle.net/Snurainiyakob/V4u5X/872/

And the UI as follows: enter image description here

You miss to initialize your $facebook -object.

Please take a look at the official documentation here . It offers nice examples to get started.

$fb = new Facebook\Facebook([
  'app_id' => '1475470649428121',
  'app_secret' => '{your-app-secret}',
  'default_graph_version' => 'v2.4',
]);

$helper = $fb->getRedirectLoginHelper();

$permissions = ['email']; // Optional permissions
$loginUrl = $helper->getLoginUrl('https://example.com/fb-callback.php', $permissions);

echo '<a href="' . htmlspecialchars($loginUrl) . '">Log in with Facebook!</a>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM