简体   繁体   中英

PHP not displaying error or echo

I have a PHP script that scrapes the meta tags and title from a webpage. It should echo them but instead does nothing. I've tried error_reporting(E_ALL) , ini_set('display_errors',1); , etc, but they do not provide any reasoning behind this.

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("http://www.tomato.co.uk/");

$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');

$title = $nodes->item(0)->nodeValue;
echo $title;

$metas = $doc->getElementsByTagName('meta');

for ($i = 0; $i < $metas->length; $i++)
{
    $meta = $metas->item($i);
    if($meta->getAttribute('name') == 'description')
        $description = $meta->getAttribute('content');
        echo $description;
    if($meta->getAttribute('name') == 'keywords')
        $keywords = $meta->getAttribute('content');
        echo $keywords;
}

Your script works for me:

php t.php

tomatotomato is a collective of artists, designers, musicians and writers engaged in publishing, exhibitions, live performances, advertising, architecture, fashion, public installations, music, television, film and graphic design.tomato is a collective of artists, designers, musicians and writers engaged in publishing, exhibitions, live performances, advertising, architecture, fashion, public installations, music, television, film and graphic design.tomato is a collective of artists, designers, musicians and writers engaged in publishing, exhibitions, live performances, advertising, architecture, fashion, public installations, music, television, film and graphic design.

If you are running via a web browser - try viewing source - this is not valid HTML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM