PHP, cCurl, and 100-Continue Response


PHP, cCurl, and 100-Continue Response

In recent months more affiliates have experienced HTTP 100-Continue responses and have been unsure how to handle them. To help, we'd like to shed some light on this situation. Lets start with a definition... This is a part of the HTTP 1.1 specification and as time passes, various technologies are becomming more compliant. RFC 2616 states that:


"Upon receiving a request which includes an Expect request-header field with the "100-continue" expectation, an origin server MUST either respond with 100 (Continue) status and continue to read from the input stream, or respond with a final status code. The origin server MUST NOT wait for the request body before sending the 100 (Continue) response. If it responds with a final status code, it MAY close the transport connection or it MAY continue to read and discard the rest of the request. It MUST NOT perform the requested method if it returns a final status code."


This process is driven by the client and when presented to servers they will endeavor to honour the associated behaviours. For PHP developers, later versions of cCurl automatically adds this header which is why you are seeing this behaviour. If you're not expecting this result, responses can appear as being null/empty because 100-Continue responses have no payload. If you want to test this yourself use the following code sample and inspect the details of $info.


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, '');

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json; charset=utf-8'

$server_output = curl_exec($ch);

$info = curl_getinfo($ch);

echo json_encode($info);


One way to stop the header from being added automatically is to explicitly set the “Except” header to nothing. If you change the sample above to include the below modification, you should see the header removed. 


curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json; charset=utf-8', 'Expect:'));


Let us know if you have any more questions