How to fix 411 Length Required error with file_get

2019-01-20 16:18发布

问题:

I am doing the online hotel booking using xml api(php) project.When i am working in the reservation code it shows error of the following

"Warning: file_get_contents(https://...@gmail.com</email><firstName>test</firstName><lastName>smith</lastName><homePhone>8870606867</homePhone><creditCardType>CA</creditCardType><creditCardNumber>5401999999999999</creditCardNumber>....</HotelRoomReservationRequest>) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 411 Length Required"

its my code

$context  = stream_context_create(
                array(
                    'http' => array(
                        'method' => 'POST',
                        'header' => "Content-type: application/x-www-form-urlencoded",
                        "Accept: application/xml"
                    )
                )
            );
$url ='https://book.api.ean.com/....';
$xml = file_get_contents($url, false, $context);

It's for sending credit information plz kindly give me suggestion what type of error... .

回答1:

As per RFC2616 10.4.12:

10.4.12 411 Length Required

   The server refuses to accept the request without a defined Content-
   Length. The client MAY repeat the request if it adds a valid
   Content-Length header field containing the length of the message-body
   in the request message.

You need to add the Content-Length header to your POST request. That's the size in bytes (octets) of your POST request body. To obtain the length you could use strlen on the POST body. As your code example does not show any POST body, it's hard to give a concrete example. The post body is passed with the ['http']['content'] entry in the stream context.

Probably it's already enough if you set the content entry (see HTTP context options­Docs).

Edit: The following example code might solve your issue. It demonstrates how to send some XML to a server via a POST request using file_get_contents and setting headers including the Content-Length header.

$url = 'https://api.example.com/action';
$requestXML = '<xml><!-- ... the xml you want to post to the server... --></xml>';
$requestHeaders = array(
    'Content-type: application/x-www-form-urlencoded',
    'Accept: application/xml',
    sprintf('Content-Length: %d', strlen($requestXML));
);

$context = stream_context_create(
                array(
                    'http' => array(
                        'method'  => 'POST',
                        'header'  => implode("\r\n", $requestHeaders),
                        'content' => $requestXML,
                    )
                )
            );
$responseXML = file_get_contents($url, false, $context);

if (FALSE === $responseXML)
{
    throw new RuntimeException('HTTP request failed.');
}

If you need better error control, see the ignore_errors HTTP context option­Docs and $http_response_header­Docs. Detailed handling of HTTP response headers is available in a blog post of mine: HEAD first with PHP Streams.



标签: php xml api http