从Facebook graphObject读取数据 [英] Reading data from Facebook graphObject
问题描述
用户已经接受了我的Facebook应用程序。我现在可以访问他们的一些数据。它作为 graphObject
返回,其中包含以下内容:
A user has accepted my Facebook app. I can now access some of their data. It is returned as a graphObject
, which contains something like:
Facebook\ GraphObject Object([backingData:protected] => Array([id] => 11111 [first_name] => Bob [gender] => male [last_name] => Builder [link] => https://www.facebook.com/app_scoped_user_id/11111/ [locale] => de_DE [name] => Bob Builder [timezone] => 2 [ updated_time] => 2014-02-14T14:35:54 + 0000 [已验证] => 1))
Facebook\GraphObject Object ( [backingData:protected] => Array ( [id] => 11111 [first_name] => Bob [gender] => male [last_name] => Builder [link] => https://www.facebook.com/app_scoped_user_id/11111/ [locale] => de_DE [name] => Bob Builder [timezone] => 2 [updated_time] => 2014-02-14T14:35:54+0000 [verified] => 1 ) )
不幸的是我无法得到在该对象内的数据。读取它像一个数组抛出一个错误:
Unfortunately I cannot get at the data inside this object. Reading it like an array throws an error:
$fbid = $graphObject['id']; // Cannot use object of type Facebook\GraphObject as array
$fbid = $graphObject->id; // Undefined property: Facebook\GraphObject::$id
如何获取ID?
推荐答案
如果您通过使用以下两种方法之一将响应转换为GraphObject:
If you have casted the response as a GraphObject by using one of the following two methods:
// Get the response typed as a GraphLocation
$loc = $response->getGraphObject(GraphLocation::className());
// or convert the base object previously accessed
// $loc = $object->cast(GraphLocation::className());
您可以使用 Get
图形对象,具体取决于您将其转换为什么样的对象...以下是 GraphUser
对象的示例:
You can use the Get
properties of the graph object, depending on what kind of object you've casted it as... here's an example for the GraphUser
Object:
echo $user->getName();
或者,如果您知道属性的名称(如基本数据所示),您可以使用 getProperty()
:
Or, if you know the name of the property (as shown in the base data), you can use getProperty()
:
echo $object->getProperty('name');
所以在您的示例中,您可以使用以下内容获取 id
property:
So in your example, you can use the following to get the id
property:
echo $user->getProperty('id');
这篇关于从Facebook graphObject读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!