Log in

View Full Version : PHP (X)HTML Validator using cURL (Not Sending Correct Request?)



JAB Creations
06-06-2008, 07:52 PM
I'm pretty sure this is not sending the correct request to the W3C (X)HTML validator. However I'm not experienced with cURL so maybe someone could help point me in the right direction please?

This script is merely checking the title element to determine if the URL submitted is valid. That's all I really need it to do! Suggestions please? :D


<?php
function check_html_compliance($url) {
$query_string = '';
foreach($_GET as $key => $val)
$query_string .= '&' . $key . '=' . $val;
if($query_string != '') {
$query_string = substr($query_string,1);
$referer = $url . '?' . $query_string;
} else
$referer = $url;
$ch = curl_init('http://validator.w3.org/check?verbose=1&uri='.$url);

curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_REFERER,$referer);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_TIMEOUT,30);
$output = curl_exec($ch);
curl_close($ch);
var_dump $ch;

if (eregi('Validation Results',$output)) {
$start = strpos($output,"<title>") + 9;
$end = strpos($output,'</title>') - $start;
$retval = html_entity_decode(substr($output,$start,$end));
return 'no page sent?';
} else if (eregi('[Invalid]',$output)) {
$start = strpos($output,"<title>") + 9;
$end = strpos($output,'</title>') - $start;
$retval = html_entity_decode(substr($output,$start,$end));
return 'valid';
} else if (eregi('[Valid]',$output)) {
$start = strpos($output,"<title>") + 9;
$end = strpos($output,'</title>') - $start;
$retval = html_entity_decode(substr($output,$start,$end));
return 'invalid';
} else {
return 'unknown error';
}
}

echo check_html_compliance($_POST['url']);
?>
<form>
<input name="url" type="text" value="http://www.google.com/" />
<input type="submit" value="Validate URL" />
</form>

JAB Creations
06-06-2008, 08:59 PM
This works fine for those following in my footsteps...

<?php
function check_html_compliance($url) {
$query_string = '';

foreach($_GET as $key => $val)
$query_string .= '&' . $key . '=' . $val;

if($query_string != '') {
$query_string = substr($query_string,1);
$referer = $url . '?' . $query_string;
}

else
$referer = $url;


$ch = curl_init('http://validator.w3.org/check?uri=referer');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_REFERER,$referer);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_TIMEOUT,10);
$output = curl_exec($ch);
curl_close($ch);

if(eregi('This page is valid',$output)) {
$start = strpos($output,'&lt;p&gt;') + 9;
$end = strpos($output,'&lt;/p&gt;') - $start;
$retval = html_entity_decode(substr($output,$start,$end));
}

else {
$retval = 'Not Valid Code';
}

return $retval;
}
echo check_html_compliance($_GET['url']);
?>
<form method="get">
<input name="url" type="text" value="http://www.google.com/" />
<input type="submit" value="Validate URL" />
</form>