注意:正在尝试获取..中非对象的属性/search.php,第46行


Notice: Trying to get property of non-object in .../search.php on line 46

有人能帮我解决这个错误吗?我已经看了一段时间了,但一直没能让它发挥作用。这是我不久前写的,上次离开时感觉它很有效,但当我重新访问它时,我还没能运行它。谢谢你的帮助。

注意:试图在/Applications/MAMP/htdocs/search/views/layouts/search.php的第46行中获取非对象的属性

警告:第46行/Applications/MAMP/htdocs/search/views/layouts/search.php中为foreach()提供的参数无效

第46行是代码最后一行的第4行。

<?php
include($_SERVER['DOCUMENT_ROOT'].'/'.'search/scripts/JSON.php');
include($_SERVER['DOCUMENT_ROOT'].'/'.'search/scripts/search_fns.php');
$searchquery = urlencode(isset($_GET['search']) ? $_GET['search'] : "news");
// Google Search API
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
. "q="
. $searchquery
."&key=ABQIAAAAYIeqEnf9yNjBzcHJK7yDdhSklBzi76D_F0lniPI7JR27aK7eCBSU-xpNs1axVS45y_PX_7_ibsScUA&userip=USERS-IP-ADDRESS&rsz=filtered_cse";
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://localhost");
$body = curl_exec($ch);
curl_close($ch);
// process the JSON string
$json = new Services_JSON;
$json = $json->decode($body);
$formattedresults = "";
$rating = "";
$search = $searchquery;
?>
<style type="text/css">
td img {display: block;}
</style>
<div id="main">
<?php
foreach($json->responseData->results as $searchresult)
{
if($searchresult->GsearchResultClass == 'GwebSearch')
{

可能返回了一个空响应。在使用foreach之前,使用is_object($json->responseData)测试变量非常有用。我还将检查属性"results"是否与property_exists($json, 'results') 一起存在