How to check Amp is enabled for a website with Php

那年仲夏 提交于 2020-01-01 19:50:08

问题


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


来源:https://stackoverflow.com/questions/45607406/how-to-check-amp-is-enabled-for-a-website-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!