0

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... .

hakre
  • 193,403
  • 52
  • 435
  • 836
user1117625
  • 17
  • 1
  • 1
  • 3

1 Answers1

6

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.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • how to do content length for my code plz help me – user1117625 Feb 23 '12 at 12:14
  • @user1117625: Well you need to change your code, it is incomplete and I can't execute it, so I can't reproduce. Sorry. – hakre Feb 23 '12 at 12:21
  • 1
    Did you even read what hakre wrote? `Content-Length` was mentioned 2 times (and even colored for your sake). It doesn't take a genius to figure out what to do and where to put it. Do people really put no effort into trying to figure out solutions these days? – N.B. Feb 23 '12 at 12:21
  • $url ='https://book.api.ean.com/ean-services/rs/hotel/v3/res?...... $str=strlen($url); $context = stream_context_create(array('http' => array('method' => 'POST','header' => "Content-type: application/x-www-form-urlencoded","Accept:application/xml",'Content-Length:$str'))); is my code correct check it – user1117625 Feb 23 '12 at 12:34
  • is it correct code?stream_context_create(array('http' => array('method' => 'POST','header' => "Content-type: application/x-www-form-urlencoded","Accept:application/xml",'Content-Length:$str'))); – user1117625 Feb 23 '12 at 13:05
  • any one help me for this code – user1117625 Feb 24 '12 at 09:41