Getting bad request with CURL in PHP posting XML data
I am using PHP CURL to post som开发者_JAVA百科e xml data to another server using that IP, i fullfilled all the requirements for making it work, but still getting an error of "BAD REQUEST".
Have a look on the code below.
$var2 ="<doc><item>Some content.</item></doc>";
$url = "server IP";
$header = "POST HTTP/1.1 \r\n";
$header .= "Content-type: text/xml \r\n";
$header .= "Content-length: ".strlen($var2)." \r\n";
$header .= "Content-transfer-encoding: text \r\n";
$header .= "Connection: close \r\n\r\n";
$header .= $var2;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POSTFIELDS, $var2);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
// Get Response
$data = curl_exec($ch);
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);
echo $data;
First, you are using this as request-line :
$header = "POST HTTP/1.1 \r\n";
According to the HTTP 1.1 RFC, the **Request-Line**
should look like this :
Request-Line = Method SP Request-URI SP HTTP-Version CRLF
So, it seems you should add an URI between POST
and HTTP/1.1
(But, before coding that, read what I've written in the next paragraphs of my answer)
Then, you are passing a lot of things as CURLOPT_CUSTOMREQUEST
.
I don't think you can pass all those header using that option (quoting) :
Valid values are things like "
GET
", "POST
", "CONNECT
" and so on;
i.e. Do not enter a whole HTTP request line here.
For instance, entering "GET /index.html HTTP/1.0\r\n\r\n
" would be incorrect.
If you want to specify custom headers (it seems you do), you should use the CURLOPT_HTTPHEADER
option, instead of CURLOPT_CUSTOMREQUEST
(quoting the documentation of the first one) :
An array of HTTP header fields to set, in the format
array('Content-type: text/plain', 'Content-length: 100')
精彩评论