如何使用facebook php SDK访问Facebook个人资料图片? [英] How to access facebook profile picture using facebook php SDK?

查看:112
本文介绍了如何使用facebook php SDK访问Facebook个人资料图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the following code to access users profile picture.

这是我如何创建请求

<?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

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

<?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());
}

我总是得到输出为array(0){} - >一个空数组。我无法弄明白这个代码有什么问题,我在访问图形查询时遇到了一些访问权限吗?

I am always getting output as "array(0) { }" -> an empty array. I cant figure out whats wrong with this code, did I missed some permission while getting login url or anything wrong with the graph query??

这里是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""
}


推荐答案

我预计有一些默认行为,但结果是需要额外的选项才能使查询成功。

I expected some default behavior but turns out that extra option is required for the query to be successful.

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

这篇关于如何使用facebook php SDK访问Facebook个人资料图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆