以下是我收到错误的代码:
for ($i = 0, $count = count($arr1); $i < $count; $i++)
{
print $arr1[$i]."\n\r\n\r\n\r\n\r<br/><br/><br/>";
$_SESSION['arrayvalue'] = "$arr1[$i]";
$in = $arr1[$i];
$in = str_replace(' ','+',$in); // space is a +
//google site search start here
function httpGet($url15)
{
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url15);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
// curl_setopt($ch,CURLOPT_HEADER, false);
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
$result15 = httpGet("https://www.google.com/cse?cx=003255331468891741234:xxxxxxxxx&client=google-csbe&output=xml_no_dtd&q='.$in.'&oq='.$in.'");
echo $result15;
//this is to get perticular tag/node value
$dom = new DomDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($result15);
$N = $dom->getElementsByTagName('U');
foreach ($N as $U) {
echo $U->nodeValue, PHP_EOL."<br/>";
}
}
这是我在显示一个结果后得到的错误:
Fatal error: Cannot redeclare httpGet() (previously declared in
/home/checkforplag/public_html/test/index.php:352) in
/home/checkforplag/public_html/test/index.php on line 352
答案 0 :(得分:0)
尝试从for循环中取出httpGet()函数。
答案 1 :(得分:0)
这个问题正在发生,因为你试图多次定义函数httpGet
(它在循环中)。只需将其移至外部,即可定义该功能并允许稍后在脚本中使用。另请参阅this answer。
//google site search start here
function httpGet($url15)
{
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url15);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
// curl_setopt($ch,CURLOPT_HEADER, false);
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
for ($i = 0, $count = count($arr1); $i < $count; $i++)
{
print $arr1[$i]."\n\r\n\r\n\r\n\r<br/><br/><br/>";
$_SESSION['arrayvalue'] = "$arr1[$i]";
$in = $arr1[$i];
$in = str_replace(' ','+',$in); // space is a +
$result15 = httpGet("https://www.google.com/cse?cx=003255331468891741234:xxxxxxxxx&client=google-csbe&output=xml_no_dtd&q='.$in.'&oq='.$in.'");
echo $result15;
//this is to get perticular tag/node value
$dom = new DomDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($result15);
$N = $dom->getElementsByTagName('U');
foreach ($N as $U) {
echo $U->nodeValue, PHP_EOL."<br/>";
}
}
另一种方法是将函数包装在if语句中以查看函数是否已存在:
if ( !function_exists('httpGet') )
{
function httpGet($url15)
{
//Function code here
}
}