Fatal Error: Cannot redeclare file_get_contents_curl() using mysql, php, curl function. Please
Fatal Error: Cannot redeclare file_get_contents_curl() using mysql, php, curl function. Please
我有这个脚本,可以用来从我拥有的几个网站中提取元标记数据。我正在尝试显示来自 3 个不同站点的 6 篇文章。这个脚本有效,但即使我的限制设置为 6,我也只能让它显示 1 篇文章。有人可以帮忙吗?
<?PHP
$sql_url = "SELECT * FROM db WHERE`approved` = 1 ORDER BY id DESC LIMIT 6";
$query_url = mysql_query($sql_url);
echo(mysql_error());
while($result_url = mysql_fetch_assoc($query_url)){
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$html = file_get_contents_curl ($result_url['url']);
//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
//get and display what you need:
$title = $nodes->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');
for ($i = 0; $i < $metas->length; $i++)
{
$meta = $metas->item($i);
if($meta->getAttribute('property') == 'og:description')
$description = $meta->getAttribute('content');
if($meta->getAttribute('property') == 'og:image')
$image = $meta->getAttribute('content');
}
?>
<article>
<a href="#"><?echo "<img src='$image' alt='$description' style='width:280px;'>";?></a>
<h1><? echo "$title";?></h1>
<p><? echo "$description";?></p>
<a href="#" class="readmore">Read more</a>
</article>
<?}?>
我目前遇到这个错误
致命错误: 无法在第 53 行的 /index.php 中重新声明 file_get_contents_curl()(之前在 /index.php:53 中声明)
错误很简单。你声明你的函数洞察你的 while 循环。每次迭代循环时,您都会定义一个新函数,但这是不正确的,并且您会收到已定义函数的错误。所以先解决这个问题。
...
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
>>>while($result_url = mysql_fetch_assoc($query_url)){
将函数放在循环之前。
我有这个脚本,可以用来从我拥有的几个网站中提取元标记数据。我正在尝试显示来自 3 个不同站点的 6 篇文章。这个脚本有效,但即使我的限制设置为 6,我也只能让它显示 1 篇文章。有人可以帮忙吗?
<?PHP
$sql_url = "SELECT * FROM db WHERE`approved` = 1 ORDER BY id DESC LIMIT 6";
$query_url = mysql_query($sql_url);
echo(mysql_error());
while($result_url = mysql_fetch_assoc($query_url)){
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$html = file_get_contents_curl ($result_url['url']);
//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
//get and display what you need:
$title = $nodes->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');
for ($i = 0; $i < $metas->length; $i++)
{
$meta = $metas->item($i);
if($meta->getAttribute('property') == 'og:description')
$description = $meta->getAttribute('content');
if($meta->getAttribute('property') == 'og:image')
$image = $meta->getAttribute('content');
}
?>
<article>
<a href="#"><?echo "<img src='$image' alt='$description' style='width:280px;'>";?></a>
<h1><? echo "$title";?></h1>
<p><? echo "$description";?></p>
<a href="#" class="readmore">Read more</a>
</article>
<?}?>
我目前遇到这个错误 致命错误: 无法在第 53 行的 /index.php 中重新声明 file_get_contents_curl()(之前在 /index.php:53 中声明)
错误很简单。你声明你的函数洞察你的 while 循环。每次迭代循环时,您都会定义一个新函数,但这是不正确的,并且您会收到已定义函数的错误。所以先解决这个问题。
...
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
>>>while($result_url = mysql_fetch_assoc($query_url)){
将函数放在循环之前。