简体   繁体   中英

Checking if website is using a valid AMP structure with DOMXPath and DomDocument

I am creating a simple function that can verify that a site structure meets the standards of a valid AMP .

For this I have to check 3 things:

  • The HTML tag goes with ⚡ attribute
  • If exists a style tag with amp-boilerplate attribute
  • If exists a script tag with src equal to https://cdn.ampproject.org/v0.js

For this I create the following code:

$htmlContent = '<!doctype html>
<html ⚡>
<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));

Unlike the last analysis ( src equal to 'https://cdn.ampproject.org/v0.js' ) analyzes that check for the lightning icon (⚡) in the html tag and the amp-boilerplate attribute in the style tag don't seem to work correctly.

How do I solve this?

Try this:

// 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;
}

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