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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?