Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rexxars/html-validator

PHP client for the Validator.nu / checker.html5.org API
https://github.com/rexxars/html-validator

Last synced: 17 days ago
JSON representation

PHP client for the Validator.nu / checker.html5.org API

Awesome Lists containing this project

README

        

html-validator
==============

PHP client for the [validator.nu](https://validator.nu/) API. Can be configured to use a self-hosted version of the API.

[![Latest Stable Version](https://img.shields.io/packagist/v/rexxars/html-validator.svg?style=flat-square)](https://packagist.org/packages/rexxars/html-validator)[![PHP Version](https://img.shields.io/badge/php-%3E%3D%205.6-8892BF.svg?style=flat-square)](https://php.net)[![License](https://img.shields.io/github/license/rexxars/html-validator.svg?style=flat-square)](https://packagist.org/packages/rexxars/html-validator)[![Build Status](https://img.shields.io/travis/rexxars/html-validator/master.svg?style=flat-square)](https://travis-ci.org/rexxars/html-validator)

Version >= 2.0.0 requires PHP >= 5.6.
Version <= 1.1.0 supports PHP <= 5.6, but won't be maintained anymore.

# Usage

```php
validateDocument($document);

$result->hasErrors(); // true / false
$result->hasWarnings(); // true / false

$result->getErrors(); // array(HtmlValidator\Message)

echo $result; // Prints all messages in human-readable format
echo $result->toHTML(); // Prints all messages HTML-formatted
```

# Installing

To include `html-validator` in your project, add it to your `composer.json` file:

```json
{
"require": {
"rexxars/html-validator": "^2.2.0"
}
}
```

# Example

Document to be validated (`validate-me.html`):
``` html

Invalid HTML4!

This document is not a proper, well-formed HTML4 document!


It contains fatal flaws, like:



  • tags which are not closed
  • span-tags which are never opened are attempted closed

  • ```

    Using the validator:
    ```php
    setParser(HtmlValidator\Validator::PARSER_HTML4);
    $result = $validator->validateDocument($document);

    echo $result;
    ```

    Output:
    ```
    info: HTML4-specific tokenization errors are enabled.

    error: End tag “li” seen, but there were open elements.
    From line 10, column 44; to line 10, column 48
    not closed

    error: Unclosed element “div”.
    From line 10, column 13; to line 10, column 17

  • tags

    error: Stray end tag “span”.
    From line 11, column 67; to line 11, column 73
    ed closed

    ```

    # Validating a URL

    Since 1.1.0 you can validate URLs as well:

    ```php
    setParser(HtmlValidator\Validator::PARSER_HTML5);
    $result = $validator->validateUrl($url);

    echo $result;
    ```

    Note that if you want to check pages that return status codes that are not in the 2xx-range (like a 404-page), you need to pass a `checkErrorPages` option:

    ```php
    $validator = new HtmlValidator\Validator();
    $validator->setParser(HtmlValidator\Validator::PARSER_HTML5);
    $result = $validator->validateUrl($url, ['checkErrorPages' => true]);

    echo $result;
    ```

    # Using a self-hosted version of the API

    Check out [validator.nu](http://about.validator.nu/#src) for instructions on setting up the service.
    Once set up, you can configure the validator to use a different host:

    ```php