如何使用facebook php SDK访问facebook个人资料图片?

时间:2014-10-17 17:12:51

标签: php facebook facebook-graph-api

我使用以下代码访问用户个人资料图片。

这是我创建请求的方式

<?php 
session_start();

include 'facebook-php-sdk/autoload.php';

use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookSession;

$permissions = array(
  'email',
  'user_location',
  'user_birthday',
  'user_photos',
  'public_profile'
);

/* Set the public and secret keys */
FacebookSession::setDefaultApplication('XXX', 'XXX');

$helper = new FacebookRedirectLoginHelper('http://localhost/oauth/login.php');
$loginUrl = $helper->getLoginUrl($permissions);
// Use the login url on a link or button to redirect to Facebook for authentication

?>
    <a href="<?php echo $loginUrl; ?>">Login using facebook</a>

<?php

&GT;

这是页面用户被重定向到..

<?php 
session_start();
include 'facebook-php-sdk/autoload.php';

use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookSession;

FacebookSession::setDefaultApplication('XXXX', 'XXXX');
$helper = new FacebookRedirectLoginHelper('http://localhost/oauth/login.php');
try {

  $session = $helper->getSessionFromRedirect();

} catch(FacebookRequestException $ex) {
// When Facebook returns an error

} catch(\Exception $ex) {
// When validation fails or other local issues

}
if ($session) {
    // Logged in


    // $request = new FacebookRequest($session, 'GET', '/me');
    // $response = $request->execute();
    // $graphObject = $response->getGraphObject();


    // var_dump($graphObject->asArray());

    $request = new FacebookRequest(
      $session,
      'GET',
      '/me/picture'
    );
    $response = $request->execute();
    $graphObject = $response->getGraphObject();

    var_dump($graphObject->asArray());
}

&GT;

我总是输出“array(0){}” - &gt;一个空数组。我无法弄清楚这段代码有什么问题,在获取登录网址或图形查询有什么问题时我是否错过了一些权限?

这是$ response对象的vardump

    It Workedobject(Facebook\FacebookResponse)#10 (5) {
  ["request":"Facebook\FacebookResponse":private]=>
  object(Facebook\FacebookRequest)#4 (6) {
    ["session":"Facebook\FacebookRequest":private]=>
    object(Facebook\FacebookSession)#8 (2) {
      ["accessToken":"Facebook\FacebookSession":private]=>
      object(Facebook\Entities\AccessToken)#3 (3) {
        ["accessToken":protected]=>
        string(221) "CAAFIKIRoZBB0BAHnkev9CzhEZA0iOCauwOQya3bz5ZBwxzG4jEM3CyK8iU4QRcZB1fdGSfBfClLSCMbGhKiguMZBTiXZB2imzZCf8XLzifaJsqjXufJ5Y    K9SxkZBboZCBWvOda4CDSNwvOf6rFW2D5qqZBZC7nFUb1tWMFTfkavkAWntBRjN6O3vZAnqkISkmpGqGqphx3JR99ca3G9gIxTiMlAn"
        ["machineId":protected]=>
        NULL
        ["expiresAt":protected]=>
        NULL
      }
      ["signedRequest":"Facebook\FacebookSession":private]=>
      NULL
    }
    ["method":"Facebook\FacebookRequest":private]=>
    string(3) "GET"
    ["path":"Facebook\FacebookRequest":private]=>
    string(3) "/me"
    ["params":"Facebook\FacebookRequest":private]=>
    array(2) {
      ["access_token"]=>
      string(221) "CAAFIKIRoZBB0BAHnkev9CzhEZA0iOCauwOQya3bz5ZBwxzG4jEM3CyK8iU4QRcZB1fdGSfBfClLSCMbGhKiguMZBTiXZB2imzZCf8XLzifaJsqjXufJ5YK9    SxkZBboZCBWvOda4CDSNwvOf6rFW2D5qqZBZC7nFUb1tWMFTfkavkAWntBRjN6O3vZAnqkISkmpGqGqphx3JR99ca3G9gIxTiMlAn"
      ["appsecret_proof"]=>
      string(64) "e30042c6689e1ca31de1057f700c95f37dda509544fed8615cc413ae085e5c08"
    }
    ["version":"Facebook\FacebookRequest":private]=>
    string(4) "v2.0"
    ["etag":"Facebook\FacebookRequest":private]=>
    NULL
  }
  ["responseData":"Facebook\FacebookResponse":private]=>
  object(stdClass)#7 (13) {
    ["id"]=>
    string(15) "703241603086465"
    ["birthday"]=>
    string(10) "08/13/1988"
    ["email"]=>
    string(19) "smit.new1@gmail.com"
    ["first_name"]=>
    string(8) "Saumitra"
    ["gender"]=>
    string(4) "male"
    ["last_name"]=>
    string(3) "Dey"
    ["link"]=>
    string(60) "https://www.facebook.com/app_scoped_user_id/703241603086465/"
    ["location"]=>
    object(stdClass)#9 (2) {
      ["id"]=>
      string(15) "110561898965802"
      ["name"]=>
      string(13) "Tezpur, Assam"
    }
    ["locale"]=>
    string(5) "en_US"
    ["name"]=>
    string(12) "Saumitra Dey"
    ["timezone"]=>
    float(5.5)
    ["updated_time"]=>
    string(24) "2014-09-12T00:44:34+0000"
    ["verified"]=>
    bool(true)
  }
  ["rawResponse":"Facebook\FacebookResponse":private]=>
  string(389) "{"id":"703241603086465","birthday":"08\/13\/1988","email":"smit.new1\u0040gmail.    com","first_name":"Saumitra","gender":"male","last_name":"Dey","link":"https:\/\/www.facebook.    com\/app_scoped_user_id\/703241603086465\/","location":{"id":"110561898965802","name":"Tezpur, Assam"},"locale":"en_US","name":"Saumitra     Dey","timezone":5.5,"updated_time":"2014-09-12T00:44:34+0000","verified":true}"
  ["etagHit":"Facebook\FacebookResponse":private]=>
  bool(false)
  ["etag":"Facebook\FacebookResponse":private]=>
  string(42) ""895f82630994e22ce349e5ab8315fe8a9c0665e1""
}

1 个答案:

答案 0 :(得分:3)

我期望一些默认行为,但事实证明,查询成功需要额外的选项。

$request = new FacebookRequest(
          $session,
          'GET',
          '/me/picture',
          array (
                'redirect' => false,
                'type' => 'large'
              )
        );