Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Curl Request returning HTTP Error 411: The request must be chunked or have a content length

Tags:

php

curl

I'm trying to send the following CURL request in PHP. But its returning: "HTTP Error 411. The request must be chunked or have a content length."

PHP Script containing the CURL Request:

<?php
$numbers = 9999999999;
$message = "Test";
$message = urlencode($message);
$port = 80;
$url = "http://191.95.51.64/API/sendsms.aspx?loginID=myloginid&password=mypassword&mobile=".$numbers."&text=".$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_RETURNTRANSFER, 1);
curl_setopt ( $ch, CURLOPT_TIMEOUT, 20 );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 20 );
$output = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);    
if ($err) {
    echo $err;
} else {
    echo $output;
}
?>  

Output:

Length Required

HTTP Error 411. The request must be chunked or have a content length.  

Since I'm new to use PHP Curl, so I couldn't be able to find out whats wrong. If anyone can briefly guide me the solution with an example, I'll be very appreciated to him. Thanks!

like image 333
Amlan Dutta Avatar asked Oct 01 '17 19:10

Amlan Dutta


People also ask

How do you pass content-length in curl?

To manually pass the Content-Length header, you need to add the Content-Length: [length] and Content-Type: [mime type] headers to your request, which describe the size and type of data in the body of the POST request.


1 Answers

Since your data seems to be send as URL parameters, try adding content length header of zero like below:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));

Also instead of touching the content length header, simply try to add an empty POST body. Based on which type of HTTP server you are posting to, the behaviour differs slightly (IIS, LightHTTPD, Apache).

Empty post body similar to:

curl_setopt($ch, CURLOPT_POSTFIELDS, array());

like image 188
raidenace Avatar answered Sep 18 '22 12:09

raidenace