Generating a SOAP request in PHP to W3C Validator?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Generating a SOAP request in PHP to W3C Validator?

Post by JAB Creations »

Using the W3C CSS Validator API I could validate CSS in the form of $_POST data to my server.

I know I would have to generate a request that includes the soap format...
http:// jigsaw.w3.org/css-validator/validator?uri=http%3A%2F%2Fwww.w3.org%2F&warning=0&profile=css2&output=soap12

What I'm vague on is...
1.) How do I generate a request by my server to the W3C SOAP API?
(How does PHP call out to the W3C server?)

2.) How do I send the W3C API the user's $_POST data?
Well I think this is part of the request to the API service right?
http://jigsaw.w3.org/css-validator/vali ... put=soap12

In that case forget about the $_POST data and let's use a static file URL...
http://www.jabcreations.com/themes/classic/style.css

I don't want to over-complicate learning how SOAP/PHP works. I can most likely generate a temporary file named as the session or something on the server (I'll more then likely post a new thread later on when/if I get to that point).

3.) How do I reference the response; is it part of the initial call by PHP/SOAP?

4.) When I have whatever the item (variable, array, SOAP-specific response item, etc) how do I use SOAP to determine validity or in the XML file that is returned how do I reference the element m:validity (which will contain either 'true' or 'false'. Or do I reference something else for validity in this case?

I am just not finding any tutorials online for PHP/SOAP and the php.net documentation is messing around with classes and functions without making it clear what is what. One example had an output of three characters and all the other tutorial pages have the exact same code for an example (making this exceptionally difficult and tedious to figure out).
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Generating a SOAP request in PHP to W3C Validator?

Post by RobertGonzalez »

cURL
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Generating a SOAP request in PHP to W3C Validator?

Post by JAB Creations »

I need something tangible code wise that I can at least in-directly replicate.

1.) How do I send a PHP SOAP request asking the validator API to validate my static CSS url?

2.) When I receive that request what do I reference to know if it validated or not?

...if someone could help me get at least that far I could probably figure out the rest with the help of a Red Bull or two. There are just no documented examples on the W3C site nor have I found anything in hours of searching online this evening.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Generating a SOAP request in PHP to W3C Validator?

Post by RobertGonzalez »

Have you read the PHP docs on cURL? Everything I learned about cURL, literally, I learned from the PHP manual.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Generating a SOAP request in PHP to W3C Validator?

Post by JAB Creations »

Err ok so...where do you suggest I start? What is the general idea of using cURL in application to my goal? I presume this will open a connection to their API? It seems better documented then SOAP?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Generating a SOAP request in PHP to W3C Validator?

Post by RobertGonzalez »

The first step is actually contacting another server with cURL. Try that. Then try posting information to that server with cURL.

Those two are at the heart of what you need to do and both of those are very clearly exampled in the manual.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Generating a SOAP request in PHP to W3C Validator?

Post by JAB Creations »

cURL--> I see close but no open, no start, no connect, etc. Where do I start?

You might as well point at a book and say, 'useful'. Why is it useful? What part makes it actually useful?

Reading randomly through things is a waste of time. I'm not looking for hand outs; if I am trying to get through a hole in a wall the size of a straw at least point out a brick or two please.

init seems to vaguely be important yet there seems to be nothing linking cURL to being able to utilize the validator.
http://www.google.com/search?hl=en&safe ... rt=10&sa=N
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Generating a SOAP request in PHP to W3C Validator?

Post by RobertGonzalez »

Look at the very first user comment from the cURL functions page of the manual. Look at the request method the user used. That gives a brief overview as to how to do it. Another way could be:

Code: Select all

<?php
// Initialize the curl session
$ch = curl_init('http://sitetoconnectto.com');
 
// This forces a post action
curl_setopt($ch, CURLOPT_POST, TRUE);
 
// Set up the post fields to pass
curl_setopt($ch, CURLOPT_POSTFIELDS, array('postfield' => 'postvalue'));
 
// Set this to get the response captured as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
 
// Execute the session and capture the response
$out = curl_exec($ch);
 
// Close the curl handle
curl_close($ch);
 
// See what is living in the response
var_dump($out);
?>
All of those functions, and more, are explained pretty well in the manual.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Generating a SOAP request in PHP to W3C Validator?

Post by JAB Creations »

Ok it looks like cURL is one way of doing this. What I intended was to comprehend what exactly cURL was doing: essentially it rips site content.

This is a partial adaptation from an existing script. It sort of works though all pages 'validate' such as Google (and anyone who knows anything about validation knows Google just does not validate).

Any way it works with the (X)HTML validator so once I figure this out it shouldn't be too difficult to adapt to the CSS validator...

Suggestions on my always-valid borked logic? :|

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,30); 
    $output = curl_exec($ch); 
    curl_close($ch); 
     
    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 $retval;
        ///$retval = 'Not Valid Code';
    } 
     
    return $retval; 
}
 
echo check_html_compliance($_POST['url']);
?>
<form>
<input name="url" type="text" value="http://www.google.com/" />
<input type="submit" value="Validate URL" />
</form>
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Generating a SOAP request in PHP to W3C Validator?

Post by JAB Creations »

Now I'm getting somewhere even if I don't exactly see how to fix the problem...but I'm able to manipulate existing code.

I was not sending a page to be validated and figured it out after I echoed out the title. Where would we be without alert and echo?

Any way I've updated the logic as so and I'm trying to figure out how it's requesting page (well the details I mean) so I can send the validation request..

Code: Select all

   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';
        ///$retval = 'Not Valid Code';
    }
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Generating a SOAP request in PHP to W3C Validator?

Post by JAB Creations »

Well this works fine and dandy for (X)HTML validation. I'm 99.9% sure I can adapt this to use the CSS validator instead. Once I get that far I'll post the CSS validation version for those following in my footsteps...

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(eregi('This page is valid',$output)) {
$start = strpos($output,'<p>') + 9;
$end = strpos($output,'</p>') - $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>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Generating a SOAP request in PHP to W3C Validator?

Post by RobertGonzalez »

Why are you using eregi() (which I believe will soon be deprecated) when all you are doing is looking for a simple sting inside of a string? Strstr() or strpos() would be way faster and easier.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Generating a SOAP request in PHP to W3C Validator?

Post by JAB Creations »

I agree with you on that, I've been using preg_match in place pretty much from the start.

Here is the (X)HTML validator...

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)) {
$start = strpos($output,'<p>') + 9;
$end = strpos($output,'</p>') - $start;
$retval = html_entity_decode(substr($output,$start,$end));
}
 
return $retval;
}
echo check_html_compliance($_GET['url']);
?>
<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>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Generating a SOAP request in PHP to W3C Validator?

Post by RobertGonzalez »

Again, preg_math is a pretty heavy function to use. strstr() (or stristr()) would do the trick a lot faster.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Generating a SOAP request in PHP to W3C Validator?

Post by JAB Creations »

Okay...LoL...how does this look?

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>';}
?>
<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>
Post Reply