首页 > 解决方案 > 使用 DOMXPath 和 DomDocument 检查网站是否使用有效的 AMP 结构

问题描述

我正在创建一个简单的函数,可以验证站点结构是否符合有效 AMP的标准。

为此,我必须检查 3 件事:

为此,我创建了以下代码:

$htmlContent = '<!doctype html>
<html ⚡&gt;
<head>
  <meta charset="utf-8">
  <link rel="canonical" href="self.html" />
  <meta name="viewport" content="width=device-width,minimum-scale=1">
  <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
  <script async src="https://cdn.ampproject.org/v0.js"></script>
</head>
<body>Hello, AMP world.2</body>
</html>';

function checkIfWebsiteIsUsingAMP($htmlContent) {

        $dom = new DOMDocument();

        libxml_use_internal_errors(true);
        $dom->loadHTML($htmlContent);
        libxml_use_internal_errors(false);

        $xpath = new DOMXPath($dom);

        $validOne = false;
        $validTwo = false;
        $validThree = false;

        //Check if html tag has ⚡
        if ($xpath->evaluate("//html ⚡")->length != 0) {
            $validOne = true;
        }

        //Check if isset style amp-boilerplate
        if ($xpath->evaluate("//style amp-boilerplate")->length != 0) {
            $validTwo = true;
        }

        //Check if has script with src equal to 'https://cdn.ampproject.org/v0.js'
        if ($xpath->evaluate("//script[@src='https://cdn.ampproject.org/v0.js']")->length != 0) {
            $validThree = true;
        }

        return array('html_with_lightning' => $validOne, 'style_with_ampboilerplate' => $validTwo, 'script_with_src' => $validThree);

}

var_dump(checkIfWebsiteIsUsingAMP($htmlContent));

与检查标记中的(⚡) 和标记中的属性的最后分析(src等于)分析似乎无法正常工作。'https://cdn.ampproject.org/v0.js'lightning iconhtmlamp-boilerplatestyle

我该如何解决这个问题?

标签: phpdomdocumentdomxpath

解决方案


尝试这个:

// Check if html tag has ⚡
if (strpos($htmlContent, '<html ⚡') !== false) {
    $validOne = true;
}

// Check if isset style amp-boilerplate
if ($xpath->evaluate("//style[@amp-boilerplate]")->length != 0) {
    $validTwo = true;
}

推荐阅读