任何人都可以帮我解决这个错误吗?一直在寻找它,但一直没能让它工作。我刚才写了这篇文章,当我上次离开它的时候感觉它很有用,但是当我再次访问它时我无法运行它。感谢您的帮助。
注意:尝试在第46行的/Applications/MAMP/htdocs/search/views/layouts/search.php中获取非对象的属性
警告:第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')
{
答案 0 :(得分:0)
可能它返回了一个空的回复。
在使用foreach之前,使用is_object($json->responseData)
测试变量很有用。我还会检查属性'结果'是否与property_exists($json, 'results')