Results 1 to 2 of 2

Thread: PHP (X)HTML Validator using cURL (Not Sending Correct Request?)

  1. #1
    Join Date
    Dec 2007
    Location
    Stranded in Sarasota Florida
    Posts
    75
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Question PHP (X)HTML Validator using cURL (Not Sending Correct Request?)

    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?

    PHP Code:
    <?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>

  2. #2
    Join Date
    Dec 2007
    Location
    Stranded in Sarasota Florida
    Posts
    75
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Default

    This works fine for those following in my footsteps...
    PHP Code:
    <?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>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •