首先让我先说我正在使用CodeIgniter和Facebook类Elliot Haughin创建。 http://www.haughin.com/code/facebook/我所做的工作一切顺利。然而,当我用各种FB帐户测试我的代码时,我偶然发现了一个我想评估的问题,但我的尝试失败了。我已经google了一下,打了其他论坛,我一直想出我最初想出的答案。应该在某种程度上起作用,但在这种情况下似乎不会......
现在我已经说过,我的问题是.. 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错误类型只不过是一个“通知”,但我通常不喜欢让我的代码处于一个状态,甚至会像一个错误那样踢回来..那就是说我迷路了..
答案 0 :(得分:1)
你没有说,但这听起来像
这样的行给出了错误$fbHomeTown = $fb_result->hometown->name;
例如<{1}}属性不存在。
阻止此通知的方法(并且你绝对应该努力争取完全没有错误的代码)是这样做的:
hometown
这会将您的临时变量设置为已知的安全值(修剪后的值或$fbHomeTown = isset($fb_result->hometown->name) ? trim($fb_result->hometown->name) : null;
)。然后,您可以通过执行
null
然而,在很多情况下(包括你的)你甚至不需要区分“不存在”和“它是一个空字符串”,你就可以轻松完成处理,如
if($fbHomeTown === null) ... // 3 equal signs