0

谁能帮我解决这个错误?一直在看它,但一直无法让它工作。前一阵子写了这个,上次离开时​​感觉它可以工作,但是当我重新访问它时,我一直无法运行它。感谢所有的帮助。

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

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

第 46 行是 foreach 代码最后一行的第 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')
{
4

1 回答 1

0

可能它返回了一个空响应。在使用 foreach 之前,使用is_object($json->responseData). 我还会检查属性“结果”是否存在property_exists($json, 'results')

于 2012-07-08T22:51:34.693 回答