php - 强制服务返回 json 而不是默认的 html

标签 php curl

我希望能够在 php 中编写与此 curl 命令等效的代码:

curl -F out=json --form-string 'content=<!DOCTYPE html><html><head><title>check it</title></head><body></body></html>' http://validator.w3.org/nu/ 

此 curl 命令按预期返回 json。 也许我在这里的文档中遗漏了一些东西: https://github.com/validator/validator/wiki/Service:-Input:-POST-bodyhttps://github.com/validator/validator/wiki/Service%3A-HTTP-interface

我现在遇到的问题是网络服务返回 html 而不是 json。 尽管我将 header 接受设置为 json,但它不起作用。我还尝试同时设置 accept 和 Content-Type 但这会触发 Web 服务的错​​误,提示输入无效。这是我需要您帮助的代码:

$html = "<!DOCTYPE html><html><head><title>test</title></head><body></body></html>";
$endPoint = "http://validator.w3.org/nu/";
$timeout = 5000;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endPoint);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $timeout);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch,CURLOPT_POSTFIELDS, array('content' => $html, 'out' => 'json'));
$output = curl_exec($ch);
if(curl_errno($ch))
{
    echo curl_error($ch);
}
curl_close($ch);    
error_log(__FILE__. ": " . __LINE__ . ": " . var_export($output, true));
echo $output;

在阅读 Ignacio 问题后,我正在更新 w3c 文档页面中的信息:

在他们的文档中,他们说 html 字符串应该在 http 正文中发送,而在他们的 java 库中,他们使用的是:

String response = null;
String source = "your html here";
HttpResponse<String> uniResponse = Unirest.post("http://localhost:8080/vnu")
    .header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36")
    .header("Content-Type", "text/html; charset=UTF-8")
    .queryString("out", "gnu")
    .body(source)
    .asString();
response = uniResponse.getBody();

这对你来说是个提示吗?只是想让你知道我都试过了

http://validator.w3.org/nu/?out=json

http://validator.w3.org/nu/

端点(作为上面 php 脚本中 $endPoint 变量的值)。

最佳答案

要获得您正在寻找的结果,您必须将数据作为 multipart/form-data 发送(您可以查看 validator page 或通过 curl 发送到的请求看到数据作为 multipart/form-data 发送),为此举个例子:

$url = 'http://validator.w3.org/nu/';
$html = '<!DOCTYPE html><html><head><title>test</title></head><body></body></html>';

$boundary = 'your-boundary'; 

$body = '--' . $boundary . "\r\n";
// set the "out" as "json"
$body .= 'Content-Disposition: form-data; name="out"' . "\r\n" . "\r\n";
$body .= 'json' . "\r\n";
$body .= "--" . $boundary ."\r\n";
// set the "content"
$body .= 'Content-Disposition: form-data; name="content"' . "\r\n" . "\r\n";
$body .= $html . "\r\n";
$body .= "--" . $boundary . "--" . "\r\n" . "\r\n";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data; boundary='.$boundary));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

echo curl_exec($ch);
curl_close($ch);

然后你会得到这样的东西:

{
    "messages": [{
            "type": "info",
            "message": "The Content-Type was “text/html”. Using the HTML parser."
        }, {
            "type": "info",
            "message": "Using the schema for HTML with SVG 1.1, MathML 3.0, RDFa 1.1, and ITS 2.0 support."
        }],
    "source": {
        "type": "text/html",
        "encoding": "utf-8",
        "code": "<!DOCTYPE html><html><head><title>test</title></head><body></body></html>"
    }
}

希望对您有所帮助。

关于php - 强制服务返回 json 而不是默认的 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34700602/

相关文章:

php - 比较页面或 CSV 文件中的关键字 : PHP ? Bash?

php - Facebook上传视频广告 : Error Code : 1363030. "Your video upload timed out before it could be completed"

php - EPP 调用上的客户端证书

php 单 curl 有效但多 curl 不起作用?

php - 如何从 SELECT 查询中获取行数?给定的对象错误 : mysqli_affected_rows() expects parameter 1 to be mysqli,

javascript - 以编码形式将图像离线存储在 HTML 文件中

php - 如何使用 php 类和对象与 mysql 数据库通信 - oop 初学者

javascript - 将 slider 箭头转换为点 slider

Webdav 的 Curl c 示例在 Apache 服务器上使用摘要身份验证

linux - 使用 CURL 添加请求时间戳