【发布时间】:2015-05-04 18:41:56
【问题描述】:
我有一个 wordpress 函数,它通过 Zebra_cURL 调用 API,然后使用 json_decode 适当地呈现数据。然而存在不稳定的行为,并且发生了以下两个错误:Trying to get property of non-object in [template file] on line 42 和第 50 行。
有问题的行如下:
//Course display callback function
function display_courses($result) {
$result->body = json_decode(html_entity_decode($result->body));
$title = $result->body[0]->{'Title'};
$term = $result->body[0]->{'Term'};
$meetings = $result->body[0]->{'Meetings'};
$status = $result->body[0]->{'Status'};
$course_number = $result->body[0]->{'OfferingName'};
$clean_course_number = preg_replace('/[^A-Za-z0-9\-]/', '', $course_number);
$credits = $result->body[0]->{'Credits'};
$instructor = $result->body[0]->{'InstructorsFullName'};
$description = $result->body[0]->{'SectionDetails'}[0]->{'Description'};
}
然后我收到一个错误Invalid argument supplied for foreach() in [template file] on line 64,即:
//Call callback function
function parse_courses($result) {
$result->body = json_decode(html_entity_decode($result->body));
$course_data = array();
foreach($result->body as $course) {
[code]
}
[more code]
}
我到底做错了什么导致这些错误?
这是the full template file 的链接。为大量的代码转储道歉,但我对此有点不知所措。
【问题讨论】: