为什么我收到无法重新声明 httpGet() 错误
why i am getting Cannot redeclare httpGet() error
下面是我出错的代码:
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
尝试将函数 httpGet() 移出 for 循环。
出现此问题是因为您试图多次定义函数 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
}
}
下面是我出错的代码:
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
尝试将函数 httpGet() 移出 for 循环。
出现此问题是因为您试图多次定义函数 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
}
}