简体   繁体   English

使用CURL PHP发布请求返回错误400? 静态错误的请求

[英]Post request using CURL PHP returning error 400? Systatically Wrong Request

I am doing a post request using cUrl and PHP, the code is mentioned below 我正在使用cUrl和PHP进行发布请求,下面提到了代码

<?php            
 $data = array("employeeAuth" => array("employeeNumber" => "376819"));
 $data_post = json_encode($data);
 echo $data_post;
 $cSession = curl_init('http://10.40.4.170:80/xxxxxx/eeee/zzzz/kkkk');
 curl_setopt($cSession,CURLOPT_CUSTOMREQUEST,"POST");
 curl_setopt($cSession,CURLOPT_POSTFIELDS,$data_post);
 curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
 curl_setopt($cSession,CURLOPT_HTTPHEADER,array(
            "Content-Type:application/json;charset=UTF-8",
            "Content-Length:". strlen($data_string)
           ));
 $result=curl_exec($cSession);
 curl_close($cSession);
 echo $result;
?>

After this code runs I get Error 400 from the server as response stating that the request is Syntactically wrong. 这段代码运行后,我从服务器收到错误400,作为响应,指出请求在语法上是错误的。 Please let me know If I am doing anything wrong. 如果我做错了任何事情,请告诉我。

You are setting Content-Length based on $data_string , but I think you meant $data_post since $data_string isn't defined anywhere. 您正在基于$data_string设置Content-Length ,但是我认为您的意思是$data_post因为$data_string没有在任何地方定义。 But, try removing the Content-Length entry all together, CURL will calculate and include it for you. 但是,尝试一起删除Content-Length条目,CURL将为您计算并包括它。 Also, include a space after the colon in each header. 另外,在每个标头中的冒号后面应包含一个空格。

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

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