简体   繁体   English

卷毛不起作用并返回400错误请求

[英]Curl not working and returning 400 Bad Request

I'm trying to make a curl request to post some data to a restful api. 我正在尝试发出curl请求以将一些数据发布到一个宁静的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. $xml包含我要发布的数据。

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? 编辑: var_dump($data)返回一个400 Bad Request ,有没有办法解决这个问题?

I think that you need to add another parameter in curl 我认为您需要在curl中添加另一个参数

curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

where $postData it's your post data $ postData是您的帖子数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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