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

致命错误:无法使用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

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.