Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZiTAL/6df9fa9485857b460b62 to your computer and use it in GitHub Desktop.
Save ZiTAL/6df9fa9485857b460b62 to your computer and use it in GitHub Desktop.
php: html validator
<?php
/*
<!-- validator.txt content -->
<form action="archivo.php" method="get">
<input id="aa" type="text" name="nombre" value="buscar" />
<input id="dd" type="submit" value="buscar" />
</form>
*/
$text = file_get_contents('validator.txt');
// html5
$text = "<!DOCTYPE html>
<html>
<meta charset=\"utf-8\">
<title>a</title>
<body>
".$text."
</body>
</html>";
$response = curl::getHeaders('http://validator.w3.org/check', array
(
'name' => 'doctype',
'prefill' => 0,
'doctype' => 'HTML5',
'group' => 0,
'fragment' => $text
),
'post',
array(CURLOPT_USERAGENT => 'curl')
);
/*
response example:
Array
(
[Date] => Wed, 04 Feb 2015 11:03:06 GMT
[Server] => Apache/2.2.16 (Debian)
[Content-Language] => en
[X-W3C-Validator-Recursion] => 1
[X-W3C-Validator-Status] => Valid
[X-W3C-Validator-Errors] => 0
[X-W3C-Validator-Warnings] => 2
[Content-Type] => text/html; charset=UTF-8
[Vary] => Accept-Encoding
[Connection] => close
[Transfer-Encoding] => chunked
)
*/
$values = getValidatorValues($response);
print_r($values);
function getValidatorValues($str)
{
$s = preg_split("/\n/", $str);
$values = array();
foreach($s as $row)
{
$s = preg_match("/^([^:]+):\s+([^$]+)$/", $row, $m);
if($m)
$values[$m[1]] = $m[2];
}
if($values)
return $values;
return false;
}
class curl
{
public static function getURL($url, $params = array(), $type = 'get', $curl_opts = array())
{
$ch = self::setCurl($url, $params, $type, $curl_opts);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
public static function getCode($url, $params = array(), $type = 'get', $curl_opts = array())
{
$ch = self::setCurl($url, $params, $type, $curl_opts);
$result = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $http_status;
}
public static function getHeaders($url, $params = array(), $type = 'get', $curl_opts = array())
{
$curl_opts[CURLOPT_HEADER] = 1;
$ch = self::setCurl($url, $params, $type, $curl_opts);
$result = curl_exec($ch);
// Then, after your curl_exec call:
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
return $header;
}
public static function setParams($array)
{
$txt = '';
if(is_array($array) && count($array)>0)
{
$txt.= '?';
$txt.= http_build_query($array);
}
return $txt;
}
private static function setCurl($url, $params = array(), $type = 'get', $curl_opts = array())
{
if(!function_exists('curl_init'))
die('curl module missing');
$type = trim(strtolower($type));
$ch = curl_init();
if($type=='post')
{
$params = http_build_query($params);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
}
else
{
$params = self::setParams($params);
curl_setopt($ch, CURLOPT_URL, $url.$params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
}
// set curl param options
if(is_array($curl_opts) && count($curl_opts)>0)
{
foreach($curl_opts as $key => $value)
{
if($key!==0)
curl_setopt($ch, $key, $value);
}
}
return $ch;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment