How to check Amp is enabled for a website with Php

2020-07-27 06:43发布

问题:

Is there a general way to check if AMP is enabled for an URL?

I currently use Curl to get HTML. What should I do after?

$html;
if(AMP is Enabled)
    echo "You are using AMP in Mobile Version";
else
    echo "You are not using AMP in Mobile Version";

回答1:

Yes you can do like this

Non AMP page contain

<link rel="amphtml" href="https://www.example.com/url/to/amp/document.html">

Assuming that you have the content in $html;

PHP CODE

    $dom = new DOMDocument();
    @$dom->loadHTML($html);

    $nodes = $dom->getElementsByTagName('link');
    foreach ($nodes as $node)
    {
        if ($node->getAttribute('rel') === 'amphtml')
        {
            echo($node->getAttribute('href'));
            /* amp page found do your logic */
        }
    }


标签: php amp-html