简体   繁体   中英

Facebook PHP SDK - api returns empty

I am trying to learn some facebook development because it seems people ask for facebook integration one way or another.

I am trying to get something as simple as my name to be echoed on screen using the PHP SDK and I cant seem to do it.

My code is extremely basic, just what's required for the

<?php
  require_once('src/facebook.php');

  $config = array(
    'appId' => '14283923YYYYYYY48',
    'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXX',
    'allowSignedRequest' => false
  );

  $facebook = new Facebook($config);
  $user_id = $facebook->getUser();
?>
<html>
  <head></head>
  <body>

  <?php
    if($user_id) {

        $user_profile = $facebook->api('/me','GET');
        echo "Name: " . $user_profile['name'];
        if (is_null($user_profile))
            echo "NULL";
        else echo "NOT NULL";
    }
  ?>

  </body>
</html>

The name does not show up, and as you can see I tested the $user_profile variable and it comes up NULL, so I assume that the api function doesn't work, doesn't return anything.

Is my code wrong? Or should I look elsewhere for the issue? Any tip or suggestion to solve the issue will be really helpful.

You need to provide a login facility to let the user explicitly allow use of their user ID, and create an access token. This can be done in the PHP SDK or JavaScript SDK (and can be both implemented for dual functionality).

Link:

https://developers.facebook.com/docs/facebook-login/

** Please also view the following: Facebook PHP SDK/Graph API - Returning MY name/info, but not anyone elses?

<?php
if($user_id) {

    $user_profile = $facebook->api('/me','GET');
    echo "Name: " . $user_profile['name'];
    if (is_null($user_profile))
        echo "NULL";
    else echo "NOT NULL";
}

?>

change the partial section of code above

$user_profile = $facebook->api('/me','GET'); to

$user profile = $facebook->api($user_id, 'GET')

then you would able to get the user profile whose using facebook login in your website

Hope this is help

if you happened to work with a old project in codeigniter 3.x & deployed in aws ELB in 2018 the facebook call back url mentioned in config or in fb app settings isn't enough, with ssl, you need to manually edit base_facebook.php and replace redirect_uri from blank to your https url in the function getAccessTokenFromCode() argument,

protected function getAccessTokenFromCode($code, $redirect_uri = 'https://www.example.com/index/fblogin') {
    if (empty($code)) {
      return false;
    }
.....

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