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="" />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>