最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Dynamic W3C Validation - Stack Overflow

programmeradmin2浏览0评论

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. If it's not then I fix any issues so it stays HTML5-valid.

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? Does the W3C offer an API for this or do you have to manually code this?

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. If it's not then I fix any issues so it stays HTML5-valid.

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? Does the W3C offer an API for this or do you have to manually code this?

Share Improve this question edited Jul 15, 2012 at 17:57 mythofechelon asked Jul 15, 2012 at 17:48 mythofechelonmythofechelon 3,80212 gold badges41 silver badges50 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Maintainer of the W3C HTML Checker (aka validator) here. In fact the checker does expose an API that lets you do, for example:

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

…which gives you the results back as JSON. There’s also a POST interface.

You can find more details here:

  • https://github./validator/validator/wiki/Service-»-HTTP-interface
  • https://github./validator/validator/wiki/Service-»-Input-»-POST-body
  • https://github./validator/validator/wiki/Service-»-Input-»-GET
  • https://github./validator/validator/wiki/Output-»-JSON

They do not have an API that I am aware of.

As such, my suggestion would be:

Send a request (GET) to the result page (http://validator.w3/check?uri=) with your page's URL (using file_get_contents() or curl). Parse the response for the valid message (DOMDocument or simple string search).

Note: This is a brittle solution. Subject to break if anything changes on W3C's side. 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 remend 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:

   $curl = curl_init();
   curl_setopt_array($curl, array(
       CURLOPT_URL => "http://validator.w3/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:

{
    "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./validator/validator/wiki/Service-»-Input-»-POST-body for more details.

发布评论

评论列表(0)

  1. 暂无评论