首先让我先说我正在使用 CodeIgniter 和 Elliot Haughin 创建的 Facebook 类。http://www.haughin.com/code/facebook/我有那个工作发现一切都很好。然而,当我使用各种 FB 帐户测试我的代码时,我偶然发现了一个我想评估的问题,但我的尝试失败了。我在谷歌上搜索并访问了其他论坛,我一直在想出我最初想出的答案。无论如何都应该起作用但在这种情况下似乎不起作用的东西..
既然我已经说过了,我的问题是.. FB 传回的对象并不总是有一个特定的条目。为了这个例子,我会说用户名..
以下是使用用户名返回的数据示例:
facebookResponse Object
(
[__construct:private] =>
[__resp] => stdClass Object
(
[data] => stdClass Object
(
[id] => 0000000000000
[name] => Random.User
[first_name] => Random
[middle_name] => ...
[last_name] => User
[link] => http://www.facebook.com/profile.php?id=000000000
[birthday] => 11/26/1980
[gender] => male
[timezone] => -4
[locale] => en_US
[verified] => 1
[updated_time] => 2011-05-30T19:24:53+0000
)
这是一个没有用户名的示例:
facebookResponse Object
(
[__construct:private] =>
[__resp] => stdClass Object
(
[data] => stdClass Object
(
[id] => 000000000000000000000
[name] => Random User
[first_name] => Random
[last_name] => User
[link] => http://www.facebook.com/Random.User
[username] => Random.User
[birthday] => 11/10/1985
[hometown] => stdClass Object
(
[id] => 000000000000000
[name] => Coventry, Connecticut
)
[location] => stdClass Object
(
[id] => 00000000000000
[name] => San Jose, California
)
我最后一次尝试解决问题的失败尝试类似于..
$fb_result = $this->facebook->call('get', 'me', array('metadata' => 1));
$fbFirstName = $fb_result->first_name;
$fbLastName = $fb_result->last_name;
$fbUserName = $fb_result->username;
$fbUserId = $fb_result->id;
$fbHomeTown = $fb_result->hometown->name;
$fbLocation = $fb_result->location->name;
echo "<strong>First Name: </strong>"; if((isset($fbFirstName))AND(!empty($fbFirstName))AND(trim($fbFirstName) !== "")){echo $fb_result->first_name ."<br />"; }else{ echo "Not found.<br />"; }
echo "<strong>Last Name: </strong>"; if((isset($fbLastName))AND(!empty($fbLastName))AND(trim($fbLastName) !== "")){echo $fb_result->last_name."<br />"; }else{ echo "Not found.<br />"; }
echo "<strong>Username: </strong>"; if((isset($fbUserName))AND(!empty($fbUserName))AND(trim($fbUserName) !== "")){echo $fb_result->username."<br />"; }else{ echo "Not found.<br />"; }
echo "<strong>User FB ID: </strong>"; if((isset($fbUserId))AND(!empty($fbUserId))AND(trim($fbUserId) !== "")){echo $fb_result->id."<br />"; }else{ echo "Not found.<br />"; }
echo "<strong>Location hometown: </strong>"; if((isset($fbHomeTown))AND(!empty($fbHomeTown))AND(trim($fbHomeTown) !== "")){echo $fb_result->hometown->name."<br />"; }else{ echo "Not found.<br />"; }
echo "<strong>Location current (manual input): </strong>"; if((isset($fbLocation))AND(!empty($fbLocation))AND(trim($fbLocation) !== "")){echo $fb_result->location->name."<br />"; }else{ echo "Not found.<br />"; }
但是,在我首先将其定义为变量然后使用 if-else 检查它的地方这样做是可行的。我现在收到“尝试获取非对象的属性”的错误消息。这意味着我想因为它不存在并且我试图将它设置为一个变量,无论它是否会为它踢回一个错误。我花了一天的大部分时间和昨天试图解决这个问题,我不得不说我被困住了。php 错误类型只不过是一个“通知”,但我通常不喜欢让我的代码处于这样的状态,这种状态甚至会像这样的错误一样踢回来.. 那就是说我迷路了..