Monday, 1 April 2019

W3 Validator Api For File Upload / Direct Input

I am trying to use the http://validator.w3.org/nu/ API for Direct Input via POST method.

https://github.com/validator/validator/wiki/Service-%C2%BB-Input-%C2%BB-textarea

This is what I tried but did not succeeded

class frontend {
    public static function file_get_contents_curl($url)
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_HEADER, 0);
        $user_agent = self::random_user_agent();
        //var_dump($user_agent);
        curl_setopt($ch,CURLOPT_USERAGENT,$user_agent);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        if (strpos($url, 'https') !== false) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        }
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}
$domain = 'yahoo.com';
$url = 'https://'.$domain;
$html = frontend::file_get_contents_curl($url);
libxml_use_internal_errors(true);
$doc = new DOMDocument;
$doc->loadHTML($html);
$html_file_output = $domain.'.html';
$dir = $_SERVER['DOCUMENT_ROOT'].'/tmp/';
if(!file_exists($dir)) {
    mkdir($dir);
}
$file_path = $dir.$html_file_output;
$doc->saveHTMLFile($file_path);
var_dump($file_path); // the filepath where the file is saved /www.domain.com/tmp/html_file.html
$url_validator = 'http://validator.w3.org/nu/';
$query = [
    'out' => 'json',
    'content' => $html // the HTML resulting from $url variable %3C%21DOCTYPE+html%3E%0....
    //'content' => $file_path tried also => /www.domain.com/tmp/the_file.html
];
$query_string = http_build_query($query);
var_dump($query_string); // returns string 'out=json&content=doctype html....' or 'out=json&content=F:/SERVER/www/www.domain.com/tmp/yahoo.com.html'
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$str_html = curl_exec($ch);
curl_close($ch);
$data = json_decode($str_html); 
var_dump($data); // returns null
unlink($file_path);



from W3 Validator Api For File Upload / Direct Input

No comments:

Post a Comment