Page 1 of 1

cURL Remote (X)HTML and CSS Validation

Posted: Fri Jun 06, 2008 11:10 pm
by JAB Creations
I'm so excited that I was able to get this all done today (technically yes today/Friday because it's 11:46pm as I'm writing this now).

Ultimately my goal is to create a working import option for my site's new CSS color scheme editor that will debut on Preview V of my website. Any one following my threads for say the past two months knows that this is pretty much a huge project for me and that I keep learning tons of stuff while I achieve creating various parts of the editor and it's features.

So any way I got curious about the possibility of passing off the regex to the CSS validator and kill two plastic flamingos with one Nerf gun. Initially I found they have a SOAP API available and I started looking for example SOAP code. However as great as it sounded the only examples were all copy and pasted from the original source: so at my still noobish level where I don't write code I have not yet learned from scratch I was like ok that isn't going to work!

Then thanks to Everah (who's name appeared in two dozen consecutive last replied by rows in the PHP code forum haha) I was messing around with the possibility of using cURL. Of course Google is our friend (and we'll let Yahoo tag along so he doesn't get threatened at the bus stop) and I found some almost working cURL code. I didn't comprehend how it was written but it was intensely clear what it was attempting to do. With lots of tweaking I was able to get the script to work as a remote (X)HTML validator which was only a slight mutation from becoming a remote CSS validator.

Any way my main goal was to be able to echo a function as valid or invalid based on the URL I wanted to validate. So when I move this from the standalone scripts you see just below I'll only have to modify it slightly to implement it as a module in to my existing setup. I love legos...

Everah also suggested using stristr instead of preg_match (and the original script used eregi). I threw in a PHP timer script to let others see the difference in the time it takes PHP to execute the two scripts.

If you guys would rather manually validate a different URL then the ones available in the select menu just replace the select menu with this following XHTML code...

Code: Select all

<input name="url" type="text" value="" />
Without further delay here is today's work for your critiquing pleasure...

Remote (X)HTML Validator (preg_match) - Not using this, just here for time comparison with the stristr (X)HTML validator example.

Code: Select all

<?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 (preg_match("/This page is valid/i", $output)) {
$retval = '<b style="color: #0f0;">Valid!</b>';
}
else {$retval = '<b style="color: #f00;">Invalid!</b>';}
 
return $retval;
//return 'result1 = '.$result1.', and result2 = '.$result2.', and result3 = '.$result3;
} //end check_html_compliance
 
if (isset($_GET['url'])) {echo 'The (X)HTML at the domain '.$_GET['url'].' is '.check_html_compliance($_GET['url']);}
else {echo '<b style="color: #00f;">Choose a domain name to validate please.</b>';}
echo "<br />\n";
$time_start = microtime(true);
// Sleep for a while
usleep(1000);
 
$time_end = microtime(true);
$time = $time_end - $time_start;
 
//echo "Rendered in  $time seconds\n";
echo 'Rendered in ';
//echo substr($time, 0, 7);
echo round($time, 7);
echo ' seconds using <b style="color: #00f;">preg_match</b>.';
?>
<form method="get">
<select name="url">
<option value="http://www.google.com/">googlecom</option>
<option selected="selected" value="http://www.jabcreations.com/">jabcreations.com</option>
</select>
<input type="submit" value="Validate URL" />
</form>

Remote (X)HTML Validator (stristr) - I eventually mutated this in to the CSS validator but this works just dandy for validating (X)HTML...

Code: Select all

<?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 (stristr($output, 'This Page Is Valid') == TRUE)
{
$retval = '<b style="color: #0f0;">Valid!</b>';
}
else {$retval = '<b style="color: #f00;">Invalid!</b>';}
 
return $retval;
}
if (isset($_GET['url'])) {echo 'The (X)HTML at the domain '.$_GET['url'].' is '.check_html_compliance($_GET['url']);}
else {echo '<b style="color: #00f;">Choose a domain name to validate please.</b>';}
echo "<br />\n";
$time_start = microtime(true);
// Sleep for a while
usleep(1000);
 
$time_end = microtime(true);
$time = $time_end - $time_start;
 
//echo "Rendered in  $time seconds\n";
echo 'Rendered in ';
//echo substr($time, 0, 7);
echo round($time, 7);
echo ' seconds using <b style="color: #00f;">stristr</b>.';
?>
<form method="get">
<select name="url">
<option value="http://www.google.com/">www.google.com</option>
<option selected="selected" value="http://www.jabcreations.com/">www.jabcreations.com</option>
</select>
<input type="submit" value="Validate URL" />
</form>

Remote CSS Validator - This works fine for what I need though if you guys want to suggest any improvements then this is the script that I will implement later this weekend for my site's import feature.

Code: Select all

<?php
function check_html_compliance($url) {
$referer = $url;
 
$ch = curl_init('http://jigsaw.w3.org/css-validator/validator?uri='.$_GET['url']);
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 (stristr($output, 'Congratulations! No Error Found.') == TRUE)
{
$result = '<b style="color: #0f0;">Valid!</b>';
}
else if (stristr($output, 'Sorry! We found the following errors') == TRUE)
{
$result = '<b style="color: #f00;">Invalid!</b>';
}
else {
$result = '<b style="color: #f00;">Error validating CSS, contact admin please.</b>';
} 
 
return $result;
}
if (isset($_GET['url'])) {echo 'The (X)HTML at the domain '.$_GET['url'].' is '.check_html_compliance($_GET['url']);}
else {echo '<b style="color: #00f;">Choose a domain name to validate please.</b>';}
echo "<br />\n";
$time_start = microtime(true);
// Sleep for a while
usleep(1000);
 
$time_end = microtime(true);
$time = $time_end - $time_start;
 
echo 'Rendered in ';
echo round($time, 7);
echo ' seconds using <b style="color: #00f;">stristr</b>.';
?>
<form method="get">
<select name="url">
<option selected="selected" value="http://www.w3.org/">www.w3.org</option>
<option value="http://us2.php.net/stristr">DevNetwork</option>
<option value="http://www.google.com/">www.google.com</option>
<option value="445566">445566</option>
</select>
<input type="submit" value="Validate URL" />
</form>

Re: cURL Remote (X)HTML and CSS Validation

Posted: Sat Jun 07, 2008 8:09 am
by JAB Creations
Here is my latest version where I save the file as the session id CSS and use it as cache to then check for the validator's response.

Keep in mind that I will be setting the $css_temp variable to a session variable as well as a cookie (hence the length limitation). This seems to pretty much work the way I want it to. I could add extra features later such as a script that emails me if the validator breaks (probably set something up with a database for that however).

Any way I'm open to suggestions for improving the code...

Code: Select all

<?php
session_name("member");
session_start();
 
function check_css_compliance($css)
{
 $referer = $css;
 $ch = curl_init('http://jigsaw.w3.org/css-validator/validator?uri='.'http://www.jabcreations.com/themes/_cache/'.session_id().'.css');
 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 (stristr($output, 'Congratulations! No Error Found.') == TRUE) {$result = 'valid';}
 else if (stristr($output, 'Sorry! We found the following errors') == TRUE) {$result = 'invalid';}
 else {$result = 'error 1';}
 return $result;
}
 
$css_temp = gzcompress($_POST['css'], 9);
 
if (isset($_POST['css']) && strlen($css_temp) < 4086)
{
 if ($_POST['css'] != '')
 {
  $fp = fopen('_cache/'.session_id().'.css', 'w');
  fwrite($fp, $_POST['css']);
  //fwrite($fp, '23');
  //echo session_id();
  fclose($fp);
 
  if (check_css_compliance($_POST['css']) == 'valid') {echo 'valid';}
  else if (check_css_compliance($_POST['css']) == 'invalid') {echo 'invalid';}
  else {echo 'error 2-validator error';}
  unlink('_cache/'.session_id().'.css');
 }
 else {echo 'blank';}
}
else {echo '4085 char length exceeded, your post had a total length after compression of <b>'.strlen($css_temp).'</b>';}
?>
<form method="post">
<textarea name="css" style="height: 128px; width: 384px;">
body {color: #fff;}
</textarea>
<input type="submit" value="Validate CSS" />
</form>

Re: cURL Remote (X)HTML and CSS Validation

Posted: Sat Jun 07, 2008 11:50 pm
by mpetrovich
I just made a post on grabbing $_POST and $_GET variables:
viewtopic.php?f=50&t=83841

That might be useful for you. I think you need to protect your $_GET variables before you process them, and $url, too.

Great work!

Re: cURL Remote (X)HTML and CSS Validation

Posted: Mon Jun 09, 2008 8:26 pm
by JAB Creations
Ops! I posted older $_GET examples (made it quicker during earlier testing). This is all now $_POST data.

Any other suggestions for improvement? Ca'mon people, I can't be that good already. :mrgreen: