Reputation: 1177
I'm trying to make a curl request to post some data to a restful api. That's the code I have:
$header = "POST HTTP/1.0 \r\n";
$header .= "Content-type: text/xml \r\n";
$header .= "Content-length: " . strlen($xml) . " \r\n";
$header .= "Content-transfer-encoding: text \r\n";
$header .= "Connection: close \r\n\r\n";
$header .= $xml;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
curl_setopt($ch, CURLOPT_POST, true);
$data = curl_exec($ch);
if (curl_errno($ch)) {
print curl_error($ch);
} else {
curl_close($ch);
echo 'success';
}
$xml
contains the data I want to post.
Well, the problem is that when I run the script it echoes 'Success' but the post is not really done.
What could be the problem here?
Thanks.
EDIT: var_dump($data)
returns a 400 Bad Request
is there any way to solve this?
Upvotes: 0
Views: 1829
Reputation: 352
I think that you need to add another parameter in curl
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
where $postData it's your post data
Upvotes: 2