【问题标题】:PHP parse Meta Tags of an aspx page failurePHP解析一个aspx页面失败的元标记
【发布时间】:2016-11-14 12:12:25
【问题描述】:

在我的项目中,我有一个解析 HTML 页面并正确检索元标记的 PHP 函数。当我为 aspx 页面运行我的函数时,即使有问题的 aspx 页面已正确设置元标记,它也会失败并且不会创建返回数据。

函数是:

function getUrlData($url)
{
$result = false;

$contents = getUrlContents($url);

if (isset($contents) && is_string($contents))
{
    $title = null;
    $metaTags = null;

    preg_match('/<title>([^>]*)<\/title>/si', $contents, $match );

    if (isset($match) && is_array($match) && count($match) > 0)
    {
        $title = strip_tags($match[1]);
    }

    preg_match_all('/<[\s]*meta[\s]*name="?' . '([^>"]*)"?[\s]*' . 'content="?     ([^>"]*)"?[\s]*[\/]?[\s]*>/si', $contents, $match);

    if (isset($match) && is_array($match) && count($match) == 3)
    {
        $originals = $match[0];
        $names = $match[1];
        $values = $match[2];

        if (count($originals) == count($names) && count($names) == count($values))
        {
            $metaTags = array();

            for ($i=0, $limiti=count($names); $i < $limiti; $i++)
            {
                $metaTags[$names[$i]] = array (
                    'html' => htmlentities($originals[$i]),
                    'value' => $values[$i]
                );
            }
        }
    }

    $result = array (
        'title' => $title,
        'metaTags' => $metaTags
    );
}

return $result;
}

function getUrlContents($url, $maximumRedirections = null, $currentRedirection = 0)
{
$result = false;

$contents = @file_get_contents($url);

// Check if we need to go somewhere else

if (isset($contents) && is_string($contents))
{
    preg_match_all('/<[\s]*meta[\s]*http-equiv="?REFRESH"?' . '[\s]*content="?[0-9]*;[\s]*URL[\s]*=[\s]*([^>"]*)"?' . '[\s]*[\/]?[\s]*>/si', $contents, $match);

    if (isset($match) && is_array($match) && count($match) == 2 && count($match[1]) == 1)
    {
        if (!isset($maximumRedirections) || $currentRedirection < $maximumRedirections)
        {
            return getUrlContents($match[1][0], $maximumRedirections, ++$currentRedirection);
        }

        $result = false;
    }
    else
    {
        $result = $contents;
    }
}

return $contents;
}

如何从 aspx 页面读取元标记?

提前致谢

上午

【问题讨论】:

    标签: javascript php html asp.net ajax


    【解决方案1】:

    使用get_meta_tags 可能有更简单的方法

    例如

    <?php
    
    // Load
    $tags = get_meta_tags('http://www.example.com/');
    
    // Debug
    echo "<pre>";
    print_r($tags);
    echo "</pre>";
    
    ?>
    

    【讨论】:

    • 你的意思是它不适用于 aspx 页面是什么意思?归根结底,服务器端编程语言(aspx、php 等)仍然产生 html 输出。使用我提到的功能有效。我用这个&lt;pre&gt;&lt;?php print_r(get_meta_tags('http://uknea.unep-wcmc.org/Resources/tabid/82/Default.aspx')); ?&gt;&lt;/pre&gt; 做了一个快速测试,我得到了预期的元标记pastebin.com/raw.php?i=KqvPZVTe - 你在你的aspx页面上试过这个功能吗?
    • 我尝试使用alcass.com/alcass/default.aspx,但它不起作用..为什么?
    • 我在您的网站/网址上尝试过,但似乎无法正常工作。我收到以下错误:Warning: get_meta_tags(http://www.alcass.com/alcass/default.aspx) [&lt;a href='function.get-meta-tags'&gt;function.get-meta-tags&lt;/a&gt;]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in test.php on line 1 - 每当对该 URL 发出 GET 请求时,您的 IIS 似乎会引发内部服务器错误 - 检查您的 IIS 日志,看看它是否有任何问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多