php - Getting a Content-Length error despite specifying content length when POSTing data to XML server -


i have strange error. i'm trying post data xml data using following code:

`$url = "http://www.example.appspot.com/clients";

$post_string = "< client>

< id>< /id>

< lastname>abcde< /lastname>

< firstname>< /firstname>

< company>< /company>

< telephone>< /telephone>

< mobile>< /mobile>

< fax>< /fax>

< website>< /website>

< email>< /email>

< addressline1>< /addressline1>

< addressline2>< /addressline2>

< addressline3>< /addressline3>

< town>< /town>

< county>< /county>

< postcode>< /postcode>

< /client>";

$header = "post http/1.0 \r\n";
$header .= "content-type: text/xml \r\n";
$header .= "content-length: ". strlen($post_string)." \r\n";
$header .= "content-transfer-encoding: text \r\n";
$header .= "connection: close \r\n\r\n";
$header .= "content-length: ". strlen($post_string)." \r\n";
$header .= $post_string;

$ch = curl_init();
curl_setopt($ch, curlopt_url,$url);

curl_setopt($ch, curlopt_userpwd, "username:password");
curl_setopt($ch, curlopt_customrequest, $header);
curl_setopt($ch, curlopt_returntransfer, 1);
curl_setopt($ch, curlopt_timeout, 4); `

but returns error:

length required post requests require content-length header.

(which curl_exec($ch))

and header data printed out (print $header):

post http/1.0 content-type: text/xml content-length: 381 < client> < id>< /id> < lastname>abcde< /lastname> < firstname>< /firstname> < company>< /company> < telephone>< /telephone> < mobile>< /mobile> < fax>< /fax> < website>< /website> < email>< /email> < addressline1>< /addressline1> < addressline2>< /addressline2> < addressline3>< /addressline3> < town>< /town> < county>< /county> < postcode>< /postcode> < /client>

wondering how solve problem i've specified content length header?! else have problem? how did solve it?

from the manual referencing curlopt_customrequest...

a custom request method use instead of "get" or "head" when doing http request. useful doing "delete" or other, more obscure http requests. valid values things "get", "post", "connect" , on; i.e. do not enter whole http request line here. instance, entering "get /index.html http/1.0\r\n\r\n" incorrect.

so, curlopt_customrequest option should set "post".

i think may want instead convert $header string array of headers pared curlopt_httpheader option, not include top post header. and, not include $post_string in headers either - belongs in request body.


Comments