php 正在检索Facebook个人资料图片的URL

lo8azlld  于 2023-08-02  发布在  PHP
关注(0)|答案(3)|浏览(179)

下面的代码有正确的输出:

$request = new FacebookRequest( $session, 'GET', '/me?fields=id,first_name,email,gender,birthday,picture.url' );
$response = $request->execute();

字符串
输出:

Facebook\GraphObject Object ( [backingData:protected] => Array ( [id] => 1412361249046879 [first_name] => Do [email] => xxxx@xxxx.com [gender] => male [birthday] => 06/30/1984 [picture] => stdClass Object ( [data] => stdClass Object ( [url] => https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpa1/t1.0-1/p50x50/10406555_1412406989042185_8978818317524743009_t.jpg ) ) ) )


然后我在html中设置了稍后要调用的变量:

$uid = $response->getGraphObject()->getProperty('id');
  $name = $response->getGraphObject()->getProperty('first_name');
  $email = $response->getGraphObject()->getProperty('email');
  **$pic = $response->getGraphObject()->getProperty('picture.data.url');**
  $sex = $response->getGraphObject()->getProperty('gender');
  $bday = $response->getGraphObject()->getProperty('birthday');


问题是$pic变量不起作用。我不明白如何调用GraphObject树中更下面的变量(即。picture->data->url)

ee7vknir

ee7vknir1#

$request = new FacebookRequest( $session, 'GET', '/me?fields=id,first_name,email,gender,birthday,picture.url' );
$response = $request->execute();      
$array = $response->getResponse();
$pic = $array->picture->data->url;

字符串
这就是最终解决它的方法。我不得不停止使用getGraphObject()函数,而只使用标准的getResponse(),这样我就可以更像对待一个常规数组一样对待它。希望这能帮到一些人。

0mkxixxg

0mkxixxg2#

你为什么不这样称呼它:

$pic = $response->getGraphObject()->getProperty('picture')->data->url;

字符串
或者使用FQL和FB SDK

$config = array(
  'appId'  => APP_ID,
  'secret' => API_SECRET,
  'cookie' => true,

);
$facebook = new Facebook($config);
$params = array(
    'method' => 'fql.query',
    'query' => "SELECT uid,email, pic, first_name, last_name, birthday, sex FROM user WHERE uid = ".$uid.""
    );

$result = $result = $facebook->api($params);


这是完整的table of user和官方的PHP SDK。希望这能帮上忙。
警告:但FQL将从新版本的FB API中删除。

ubof19bj

ubof19bj3#

谢谢你,9年后的今天:)

相关问题