2014-02-21 105 views
0

我有一个PHP脚本,从网页中刮去元标记和标题。它应该回应他们,但是什么都不做。我试过error_reporting(E_ALL)ini_set('display_errors',1);等,但他们不提供任何推理背后。PHP不显示错误或回声

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; 
} 
+0

难道是你在这里压制错误:@ $ doc-> loadHTML($ html); ? – Kei

+0

当我在这里运行它时,你的脚本就可以工作。检查你的服务器日志,可能有东西在那里报告。 – Barmar

+0

@Kei你会推荐删除吗? – user3339235

回答

-1

您的脚本对我的作品:

PHP t.php

tomatotomato是艺术家,设计师,音乐家和作家 从事出版的集体,展览,现场表演, 广告,建筑,时尚,公共设施,音乐, 电视,电影和平面design.tomato是一个艺术家集体, 设计师,音乐家和作家从事出版,前 现场表演,广告,建筑,时尚,公众 装置,音乐,电视,建筑, 时尚,公共设施,音乐,电视,电影和图形 设计。

如果您通过网络浏览器运行 - 请尝试查看源代码 - 这不是有效的HTML。

+0

你是如何运行脚本的? – user3339235

+0

我从命令行运行它 - 我将您的代码复制到一个文件t.php,然后使用php命令行运行它 - php t.php – BillyBigPotatoes

+0

也适用于我这种方式。这可能是我的主机问题吗? – user3339235