如何使用 PHP 检查网站是否启用了 Amp [英] How to check Amp is enabled for a website with Php

查看:27
本文介绍了如何使用 PHP 检查网站是否启用了 Amp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种通用的方法可以检查某个网址是否启用了 AMP?

我目前使用 Curl 来获取 HTML.之后我该怎么办?

$html;如果(AMP 已启用)回声您在移动版本中使用 AMP";别的回声您没有在移动版本中使用 AMP";

解决方案

是的,你可以这样做

非 AMP 网页包含

<块引用>

假设你有 $html 中的内容;

PHP 代码

 $dom = new DOMDocument();@$dom->loadHTML($html);$nodes = $dom->getElementsByTagName('link');foreach ($nodes 作为 $node){if ($node->getAttribute('rel') === 'amphtml'){echo($node->getAttribute('href'));/* 放大器页面找到你的逻辑 */}}

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";

解决方案

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆