简体   繁体   中英

Curl not working and returning 400 Bad Request

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?

I think that you need to add another parameter in curl

curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

where $postData it's your post data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM