dqzlqfqeh845799833 2015-12-25 21:53 采纳率: 0%
浏览 69
已采纳

致命错误:无法使用mysql,php,curl函数重新声明file_get_contents_curl()。 请[关闭]

I have this script that i'm using to pull meta tag data from a few sites I own. I'm trying to display 6 total articles from 3 different sites. This script works, but I can only get it to display 1 article even though my limit is set at 6. Can anyone help please?

<?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>
<?}?>

I'm currently getting this error Fatal Error: Cannot redeclare file_get_contents_curl() (previously declared in /index.php:53) in /index.php on line 53

  • 写回答

1条回答 默认 最新

  • dongyuan1870 2015-12-25 23:58
    关注

    The error is very simple. You declare your function insight your while loop. Every time you iterate through your loop you define a new function and that is not correct and you get the error that your function is already defined. So fix that problem first.

    ...
    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)){
    

    Put your function before your loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀