简体   繁体   English

动态W3C验证

[英]Dynamic W3C Validation

I have a small element on my website that displays the validity of the current page's markup. 我的网站上有一个小元素,用于显示当前页面标记的有效性。 At the moment, it is statically set as "HTML5 Valid", as I constantly check whether it is, in fact, HTML5 valid. 目前,它被静态设置为“ HTML5有效”,因为我不断检查它是否实际上是HTML5有效。 If it's not then I fix any issues so it stays HTML5-valid. 如果不是,那么我将修复所有问题,使其保持HTML5合法。

I would like this element to be dynamic, though. 我希望这个元素是动态的。 So, is there any way to ping the W3C Validation Service with the current URL, receive the result and then plug the result into a PHP or JavaScript function? 因此,是否有任何方法可以使用当前URL ping W3C验证服务,接收结果,然后将结果插入PHP或JavaScript函数? Does the W3C offer an API for this or do you have to manually code this? W3C是否为此提供API,还是必须手动编码?

Maintainer of the W3C HTML Checker (aka validator) here. W3C HTML Checker(又名验证器)的维护者。 In fact the checker does expose an API that lets you do, for example: 实际上,检查器确实公开了可以让您执行的API,例如:

https://validator.w3.org/nu/?doc=https%3A%2F%2Fgoogle.com%2F&out=json https://validator.w3.org/nu/?doc=https%3A%2F%2Fgoogle.com%2F&out=json

…which gives you the results back as JSON. …将结果返回为JSON。 There's also a POST interface. 还有一个POST界面。

You can find more details here: 您可以在此处找到更多详细信息:

They do not have an API that I am aware of. 他们没有我所知道的API。

As such, my suggestion would be: 因此,我的建议是:

Send a request ( GET ) to the result page ( http://validator.w3.org/check?uri= ) with your page's URL (using file_get_contents() or curl ). 使用页面的URL(使用file_get_contents()curl )将请求( GET )发送到结果页面( http://validator.w3.org/check?uri= )。 Parse the response for the valid message ( DOMDocument or simple string search). 解析响应以获取有效消息( DOMDocument或简单字符串搜索)。

Note: This is a brittle solution. 注意:这是一种脆弱的解决方案。 Subject to break if anything changes on W3C's side. 如果W3C方面发生任何更改,则可能会中断。 However, it will work and this tool has been available for several years. 但是,它将起作用,并且该工具已经使用了几年。

Also, if you truly want this on your live site I'd strongly recommend some kind of caching. 另外,如果您确实希望在现场站点上使用此功能,则强烈建议您使用某种缓存。 Doing this on every page request is expensive. 在每个页面请求上执行此操作都很昂贵。 Honestly, this should be a development tool. 老实说,这应该是一种开发工具。 Something that is run and reports the errors to you. 正在运行并向您报告错误的内容。 Keep the badge static. 保持徽章静止。

Here is an example how to implement W3C API to validate HTML in PHP: 这是一个示例,该示例如何实现W3C API来验证PHP中的HTML:

   $curl = curl_init();
   curl_setopt_array($curl, array(
       CURLOPT_URL => "http://validator.w3.org/nu/?out=json",
       CURLOPT_RETURNTRANSFER => true,
       CURLOPT_ENCODING => "",
       CURLOPT_MAXREDIRS => 10,
       CURLOPT_TIMEOUT => 30,
       CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
       CURLOPT_CUSTOMREQUEST => "POST",
       CURLOPT_POSTFIELDS => '<... your html text to validate ...>',
       CURLOPT_HTTPHEADER => array(
           "User-Agent: Any User Agent",
           "Cache-Control: no-cache",
           "Content-type: text/html",
           "charset: utf-8"
       ),
   ));
   $response = curl_exec($curl);
   $err = curl_error($curl);
   curl_close($curl);
   if ($err) {
      //handle error here
      die('sorry etc...');
   }
   $resJson = json_decode($response, true);

$resJson will look like this: $ resJson将如下所示:

{
    "messages": [
        {
            "type": "error",
            "lastLine": 13,
            "lastColumn": 110,
            "firstColumn": 5,
            "message": "Attribute “el” not allowed on element “link” at this point.",
            "extract": "css\">\n    <link el=\"stylesheet\" href=\"../css/plugins/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.min.css\">\n    <",
            "hiliteStart": 10,
            "hiliteLength": 106
        },
        {
            "type": "info",
            "lastLine": 294,
            "lastColumn": 30,
            "firstColumn": 9,
            "subType": "warning",
            "message": "Empty heading.",
            "extract": ">\n        <h1 id=\"promo_codigo\">\n     ",
            "hiliteStart": 10,
            "hiliteLength": 22
        },....

Check https://github.com/validator/validator/wiki/Service-»-Input-»-POST-body for more details. 检查https://github.com/validator/validator/wiki/Service-»-Input-»-POST-body以获得更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM