Forced service to return json instead of standard html

I want to be able to code in php the equivalent of this curl command:

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

This curl command returns json as expected. Maybe I missed something from their documentation here: https://github.com/validator/validator/wiki/Service:-Input:-POST-body and https://github.com/validator/validator/wiki / Service% 3A-HTTP-interface

Now the problem is that the web service is returning html instead of json. Although I am setting the header to accept json, it does not work. I also tried setting both accept and Content-Type, but this is causing an error from a web service that specifies invalid input. Here is the code I need your help in:

$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/

endpoints ( $endPoint php script ).

+4
1

, , multipart/form-data ( , 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>"
    }
}

, .

+1
source

Source: https://habr.com/ru/post/1623609/


All Articles