Skip to content

Instantly share code, notes, and snippets.

@tap52384
Last active January 15, 2016 15:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tap52384/60cc27fd0a76869cc38b to your computer and use it in GitHub Desktop.
Save tap52384/60cc27fd0a76869cc38b to your computer and use it in GitHub Desktop.
Uses the official w3c validator to validate HTML snippets. The results are returned as a JSON string. Could be refactored, but it works as is.
/**
* Sends an HTML snippet to the W3C validator and returns the result as JSON.
* @param string $html HTML code
* @return string
* @link https://github.com/validator/validator/wiki/Service:-Input:-POST-body
* @link https://gist.github.com/tap52384/60cc27fd0a76869cc38b#file-php_html_validator-php
*/
public static function validateHtml($html = '')
{
$default = array(
'messages' => array(),
'source' => array(
'type' => 'text/html',
'encoding' => 'utf-8',
'code' => ''
)
);
$default = json_encode($default);
// replace special characters
$html = str_replace("\r", "", $html);
$html = str_replace("\n", "", $html);
if (!is_string($html) || empty($html) || strlen(trim($html)) === 0) {
return $default;
}
// insert the code within a complete HTML document
$html = '<!DOCTYPE html><html><head><title>Test Document</title></head><body>'
. $html . '</body></html>';
// set the headers, including the required multipart boundary
$headers = array(
'Content-Type: multipart/form-data; boundary=---------------------------' .
strlen($html),
'User-Agent: ' . getenv('HTTP_USER_AGENT')
);
// url for the validator
$url = 'https://validator.w3.org/nu/?out=json&parser=html5';
// options for the validator
$content = array(
'parser' => 'html5',
'out' => 'json',
'showsource' => 'yes',
'asciiquotes' => 'yes',
'content' => $html
);
// send the html to the w3c validator via curl and return the result
// initiate cURL (a cURL handle)
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_USERAGENT, getenv('HTTP_USER_AGENT'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if (!is_int($ch) &&
$ch !== false &&
$ch !== null &&
!is_bool($ch)) {
curl_close($ch);
}
return $result !== false ? $result : $default;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment