3

I'm trying to use a web service with php curl but I'm getting the http code "100" and an "Recv failure: Connection was reset" error.. I tested the web service with google chrome's extension "Postman -REST Client" and it works perfectly. I searched this http code and it looks like the server is expecting a body request data but I don't know what it's expecting me to send since I'm not posting any data, just the headers.

My code looks like this:

error_reporting(-1);

    $header = array('Content-Length: 1831', 'Content-Type: application/json', 'Authorization: '. $authorization, 'Authorization-Admin: '. $authorization_admin);
    $url = 'http://api.bookingreports.com/public/1/analytics/reports/departures';


    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);


    $result = curl_exec($ch); 
    $errorCode = curl_getinfo($ch, CURLINFO_HEADER_OUT);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    echo $result;
    echo curl_error($ch);
    echo $errorCode;
    echo $httpcode;

    //log_message('debug', 'HTTP HEADER '. $errorCode); 
    curl_close($ch);

and the header I'm sending looks like this:

POST /public/1/analytics/reports/departures HTTP/1.1
Host: api.bookingreports.com
Accept: */*
Content-Length: 1831
Content-Type: application/json
Authorization: myauthcode
Authorization-Admin: myauthadmin
Expect: 100-continue
Mauricio
  • 839
  • 2
  • 13
  • 26
  • I noticed that cURL added an extra `Expect` header for you, so I think this question may probably help you: http://stackoverflow.com/questions/14158675/how-can-i-stop-curl-from-using-100-continue – Inglis Baderson Aug 20 '13 at 01:35
  • @AgreeOrNot I had already tried those two solutions.. FAILONERROR doesn't do any difference and if I remove the Expect from the header the http code of the response is "0", which I'm aware means host not found.. I don't understand this because like I said I've already tested this web service with google chrome's REST CLIENT extension and it works perfectly.. – Mauricio Aug 20 '13 at 02:48
  • Could the problem be `Content-Length: 1831`? It should be `0` if you don't post any data. – Inglis Baderson Aug 20 '13 at 02:57
  • @AgreeOrNot also I just noticed that when I remove the Expect from the header curl changes in the header output POST for HEAD, so it looks like this "HEAD /public/1/analytics/reports/departures HTTP/1.1".. this must be the problem because the api requires the POST method.. Any idea why curl is doing this or how to fix it? – Mauricio Aug 20 '13 at 03:04
  • @AgreeOrNot Problem solved! I changed content-length to 0 like you said and solved the HEAD problem thanks to this question http://stackoverflow.com/questions/9474971/php-curl-cannot-set-post-method .. I had added this line curl_setopt($ch, CURLOPT_NOBODY, true); trying stuff.. Thank you very much1 – Mauricio Aug 20 '13 at 03:15
  • Good, I'm glad I helped. – Inglis Baderson Aug 20 '13 at 03:22
  • @AgreeOrNot You can write your `Content-Length` solution as an answer so we can upvote it as the correct solution, and so Mauricio can accept it as the correct solution. – Kristian Aug 20 '13 at 18:35

1 Answers1

3

You're sending a wrong Content-Length value. That's why the server expects a request body. Because you aren't sending any data, Content-Length should be set to 0.

$header = array('Content-Length: 0', 'Content-Type: application/json', 'Authorization: '. $authorization, 'Authorization-Admin: '. $authorization_admin);

When you decide to compose request header manually, make sure Content-Length is set to a correct value.

Inglis Baderson
  • 779
  • 4
  • 12