Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用于文件上传/直接输入的W3验证程序Api_Php_Api_Curl_Post_Http Post - Fatal编程技术网

Php 用于文件上传/直接输入的W3验证程序Api

Php 用于文件上传/直接输入的W3验证程序Api,php,api,curl,post,http-post,Php,Api,Curl,Post,Http Post,我正在尝试使用http://validator.w3.org/nu/API,用于通过POST方法直接输入 这是我尝试过的,但没有成功 class frontend { public static function file_get_contents_curl($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); $user_agent = self::ra

我正在尝试使用
http://validator.w3.org/nu/
API,用于通过POST方法直接输入

这是我尝试过的,但没有成功

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);
首先,“直接输入”api只接受
multipart/form data
-格式的POST请求,但当您通过
http\u build\u query()
运行它时,您会将其转换为
application/x-www-form-urlencoded
-格式,而该api并不理解。(给CURLOPT_POSTFIELDS一个数组,它会自动转换为
多部分/表单数据

其次,此API阻止缺少
用户代理
头的请求,并且libcurl没有默认UA(cli程序有curl,但libcurl没有),因此您必须自己提供一个,但不提供

。。。修复这两个问题,并添加一些简单的错误消息解析

<?php
$ch=curl_init();
$html=<<<'HTML'
<!DOCTYPE html>
<html lang="">
<head>
<title>Test</title>
</head><ERR&OR
<body>
<p></p>
</body>
</html>
HTML;
curl_setopt_array($ch,array(
    CURLOPT_URL=>'http://validator.w3.org/nu/',
    CURLOPT_ENCODING=>'',
    CURLOPT_USERAGENT=>'PHP/'.PHP_VERSION.' libcurl/'.(curl_version()['version']),
    CURLOPT_POST=>1,
    CURLOPT_POSTFIELDS=>array(
        'showsource'=>'yes',
        'content'=>$html
    ),
    CURLOPT_RETURNTRANSFER=>1,
));
$html=curl_exec($ch);
curl_close($ch);
$parsed=array();
$domd=@DOMDocument::loadHTML($html);
$xp=new DOMXPath($domd);
$res=$domd->getElementById("results");
foreach($xp->query("//*[@class='error']",$res) as $message){
    $parsed['errors'][]=trim($message->textContent);
}
var_dump($html);
var_dump($parsed);
这使得它可以打印:

array(1) {
  ["errors"]=>
  array(4) {
    [0]=>
    string(147) "Error: Saw < when expecting an attribute name. Probable cause: Missing > immediately before.At line 6, column 1</head><ERR&OR↩<body>↩<p></p>↩"
    [1]=>
    string(245) "Error: Element err&or not allowed as child of element body in this context. (Suppressing further errors from this subtree.)From line 5, column 8; to line 6, column 6e>↩</head><ERR&OR↩<body>↩<p></Content model for element body:Flow content."
    [2]=>
    string(135) "Error: End tag for  body seen, but there were unclosed elements.From line 8, column 1; to line 8, column 7>↩<p></p>↩</body>↩</htm"
    [3]=>
    string(109) "Error: Unclosed element err&or.From line 5, column 8; to line 6, column 6e>↩</head><ERR&OR↩<body>↩<p></"
  }
}
array(1) {
  ["errors"]=>
  array(4) {
    [0]=>
    string(138) "Error: Saw < when expecting an attribute name. Probable cause: Missing > immediately before.At line 6, column 1</head><ERR&OR<body><p></p>"
    [1]=>
    string(236) "Error: Element err&or not allowed as child of element body in this context. (Suppressing further errors from this subtree.)From line 5, column 8; to line 6, column 6e></head><ERR&OR<body><p></Content model for element body:Flow content."
    [2]=>
    string(126) "Error: End tag for  body seen, but there were unclosed elements.From line 8, column 1; to line 8, column 7><p></p></body></htm"
    [3]=>
    string(100) "Error: Unclosed element err&or.From line 5, column 8; to line 6, column 6e></head><ERR&OR<body><p></"
  }
}
数组(1){
[“错误”]=>
阵列(4){
[0]=>
字符串(138)“错误:Saw<当需要属性名时。可能的原因:缺少>紧接在前面。在第6行第1列

array(1) {
  ["errors"]=>
  array(4) {
    [0]=>
    string(147) "Error: Saw < when expecting an attribute name. Probable cause: Missing > immediately before.At line 6, column 1</head><ERR&OR↩<body>↩<p></p>↩"
    [1]=>
    string(245) "Error: Element err&or not allowed as child of element body in this context. (Suppressing further errors from this subtree.)From line 5, column 8; to line 6, column 6e>↩</head><ERR&OR↩<body>↩<p></Content model for element body:Flow content."
    [2]=>
    string(135) "Error: End tag for  body seen, but there were unclosed elements.From line 8, column 1; to line 8, column 7>↩<p></p>↩</body>↩</htm"
    [3]=>
    string(109) "Error: Unclosed element err&or.From line 5, column 8; to line 6, column 6e>↩</head><ERR&OR↩<body>↩<p></"
  }
}
foreach($xp->query("//*[@class='lf']") as $remove){
    $remove->parentNode->removeChild($remove);
}
array(1) {
  ["errors"]=>
  array(4) {
    [0]=>
    string(138) "Error: Saw < when expecting an attribute name. Probable cause: Missing > immediately before.At line 6, column 1</head><ERR&OR<body><p></p>"
    [1]=>
    string(236) "Error: Element err&or not allowed as child of element body in this context. (Suppressing further errors from this subtree.)From line 5, column 8; to line 6, column 6e></head><ERR&OR<body><p></Content model for element body:Flow content."
    [2]=>
    string(126) "Error: End tag for  body seen, but there were unclosed elements.From line 8, column 1; to line 8, column 7><p></p></body></htm"
    [3]=>
    string(100) "Error: Unclosed element err&or.From line 5, column 8; to line 6, column 6e></head><ERR&OR<body><p></"
  }
}