PHP - 获取元标记

时间:2011-12-28 13:20:22

标签: php

我有这段代码:

<?php

$LF = 'links.txt';
$Rfp = fopen($LF, 'r');

while(!feof($Rfp))
{
    $links = fgets($Rfp);
    $tags = get_meta_tags($links);

    if ($tags['description'] == "")
        echo "No META Description Found";
    else
        echo $tags['description'];
}

fclose($Rfp);

?>

我的links.txt每行有一个5000个URL的列表。当我执行此脚本

时,我的文件中的每个URL都会出现此错误
Warning: get_meta_tags(http://www.1saleaday.com ) [function.get-meta-tags]: failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known. in F:\xampp\htdocs\mte.php on line 10
No META Description Foundhttp://www.1stopflorists.com
Warning: get_meta_tags() [function.get-meta-tags]: php_network_getaddresses: getaddrinfo failed: No such host is known. in F:\xampp\htdocs\mte.php on line 10

我确信我在links.txt文件中的链接都有META描述。那么,我在哪里失败了?请指导。

3 个答案:

答案 0 :(得分:4)

以下是我的工作示例: links.txt看起来像这个例子

http://www.site1.com
http://www.site2.com
http://www.site2.com/page1

每行后没有空格,文件末尾没有中断

 <?php

$LF = 'links.txt';
$Rfp = fopen($LF, 'r');

while(!feof($Rfp))
{
    $links = fgets($Rfp);
    echo "<br>"; echo "<font color=\"red\">Load URL: $links </font>"; $tags = get_meta_tags($links); echo "<br>";

    if ($tags['description'] == "")
    echo "No META Description Found";
    else
        echo "<br>"; 
    echo "<b>Description</b> - "; 
    echo $tags['description']; 
    echo "<br>";

    if ($tags['keywords'] == "")
        echo "No META Keywords Found";
    else
        echo "<br>";
    echo "<b>Keywords</b> - ";
    echo $tags['keywords'];
    echo "<br>";
}

fclose($Rfp);

?>

结果看起来很好,没有错误

答案 1 :(得分:2)

您需要的答案就在您包含的警告输出中......请注意URL后面的空格:

警告:get_meta_tags(http://www.example.com)[function.get-meta-tags]

如果你添加:

$ links = trim($ links);

您的代码将开始工作。

答案 2 :(得分:1)

  

php_network_getaddresses:getaddrinfo failed:没有这样的主机

显然,PHP无法加载页面。也许链接没有正确的格式?

快速测试显示,当我尝试get_meta_tags('http://domain.com http://another-domain.com');时,会发生这种情况,或许换行有问题? (Windows vs Unix line-endings?)