HTTP 411: Diagnosing and Fixing "Length Required" in PHP cURL
When a PHP script throws an HTTP Error 411: Length Required, the server is explicitly rejecting the request because it cannot determine how much data to expect. This specific status code indicates that the client failed to specify a Content-Length header or did not use chunked transfer encoding for the body of the request.
In the scenario you described, the script attempts to send an SMS payload via a POST request. The error arises because the cURL configuration does not include any data in the request body, yet the target endpoint expects one. Specifically, the code initializes cURL with CURLOPT_POST set to 1, signaling that the server should expect a POST body. However, no arguments are passed to curl_setopt to define what that body contains or how long it is.
The server at 191.95.51.64/API/sendsms.aspx likely validates incoming SMS requests by checking for the presence of message parameters (like text, mobile, etc.). Even though these parameters are present in the query string, the request method is POST. In many legacy API designs or specific gateway configurations, parameters intended for the body must be explicitly sent as part of the POST payload, not just appended to the URL.
Adding the Request Body
To resolve this, you must construct a string containing the data and pass it to cURL using CURLOPT_POSTFIELDS. This action automatically sets the Content-Type header (usually to application/x-www-form-urlencoded) and calculates the Content-Length, satisfying the server's requirement.
Here is how to modify your script:
<?php
$numbers = '9999999999'; // Ensure this is a string, not an integer without quotes in logic
$message = "Test";
// Construct the data array for POST body
$data = [
'loginID' => 'myloginid',
'password' => 'mypassword',
'mobile' => $numbers,
'text' => $message,
'senderid' => 'ABCDEF',
'route_id' => '1',
'Unicode' => '1'
];
$port = 80;
// You generally don't need to append data to the URL if sending via POST body.
// However, if the API strictly requires these in the query string AND the body,
// you would keep the URL as is, but adding CURLOPT_POSTFIELDS is critical.
$url = "http://191.95.51.64/API/sendsms.aspx?loginID=myloginid&password=mypassword&mobile=" . $numbers . "&text=" . urlencode($message) . "&senderid=ABCDEF&route_id=1&Unicode=1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PORT, $port);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); // This is the fix
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
$output = curl_exec($ch);
$err = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($err) {
echo "cURL Error: {$err}";
} else {
// It is best practice to check the HTTP status code as well
if ($httpCode == 411) {
echo "Server rejected request due to missing length (logic error above).";
} else {
echo $output;
}
}
?>
Why CURLOPT_POSTFIELDS Solves It
By passing a string or an array to CURLOPT_POSTFIELDS, cURL performs the following behind the scenes:
- Encodes the Data: If you pass an array, cURL serializes it into
application/x-www-form-urlencodedformat. - Calculates Length: cURL determines the byte size of the serialized string and injects a
Content-Length: <size>header automatically. - Sets Content-Type: It adds the appropriate MIME type header.
Without this step, cURL sends an empty body for a POST request. Some servers interpret an empty POST body as invalid when parameters are expected, triggering the 411 response because they cannot validate the content length against their schema.
Handling Large Payloads and Binary Data
If your SMS messages contain Unicode characters or large binary attachments, the default form encoding might not suffice. In such cases, you might need to set CURLOPT_POSTFIELDS to a raw string with a specified Content-Type (e.g., text/plain or application/octet-stream). For binary data, setting CURLOPT_BINARYTRANSFER ensures PHP handles the output correctly.
Additionally, ensure that the URL parameters are properly encoded using urlencode() before concatenation, as seen in your original code for the $message variable. While this prevents double-encoding issues, it is strictly necessary only if you are not relying on cURL's built-in encoding when using CURLOPT_POSTFIELDS. If you mix query string parameters and POST body data, consistency is key to avoiding parsing errors on the server side.
Troubleshooting Persistent 411 Errors
If implementing CURLOPT_POSTFIELDS does not resolve the issue, verify the following:
- Server Expectations: Some APIs require specific header values like
Content-Type: application/json. If the server expects JSON but receives form data, it might reject the request. You can force this by settingcurl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json'])and sending a JSON string toCURLOPT_POSTFIELDS. - SSL Verification: Ensure you are not inadvertently skipping SSL verification if the endpoint requires HTTPS (though your URL uses HTTP).
- Redirects: Sometimes intermediate proxies strip headers. Setting
CURLOPT_FOLLOWLOCATIONmight help if the server redirects to a different endpoint that expects data in a specific format.
Understanding exactly what the server expects regarding request body size and structure is crucial when debugging 411 errors. Once the content length is explicitly defined via cURL options, the protocol error disappears.