脚本宝典收集整理的这篇文章主要介绍了php – 强制服务返回json而不是默认的html,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
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-body和https://github.com/validator/validator/wiki/Service%3A-HTTP-interface
我现在的问题是Web服务返回html而不是json.
虽然我将标题接受设置为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,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变量的值).
$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,$url); curl_setopt($ch,array('Content-Type: multipart/form-data; boundary='.$boundary)); curl_setopt($ch,$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全部内容,希望文章能够帮你解决php – 强制服务返回json而不是默认的html所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。