简体   繁体   English

Facebook的:如何在PHP中检索我自己的照片?

[英]facebook: how to retrive my own photos in php?

I am trying to display my own photos form my facebook page. 我正在尝试从我的Facebook页面显示我自己的照片。 I am using the latest FB SDK. 我正在使用最新的FB SDK。

I have various photos in my FB. 我的FB中有各种照片。

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

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

// 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');
    $photos = $facebook->api('me/photos');
    print_r($photos);

  } 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 {
  $statusUrl = $facebook->getLoginStatusUrl();
  $loginUrl = $facebook->getLoginUrl();
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">

  <body>
    <h1>php-sdk</h1>
    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <div>
        Check the login status using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo $statusUrl; ?>">Check the login status</a>
      </div>
      <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 ?>

  </body>
</html>

However $photos is empty: what Am I doing wrong ? 但是$ photos是空的:我在做什么错? Do I need any special rights for my application ? 我的申请需要任何特别权利吗?

在此处输入图片说明

Adding the permissions in the App Settings is for the App Centre only, the permissions will actually be asked in the app only if you add them in your login code. 在“应用程序设置”中添加权限仅适用于App Center,仅当您在登录代码中添加权限后,实际上才会在应用程序中询问这些权限。

So, replace- 因此,更换-

$loginUrl = $facebook->getLoginUrl();

with- 与-

$params = array(
  'scope' => 'user_photos'
);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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