2014-01-10 149 views
0

我做使用卷曲和PHP POST请求,代码如下使用CURL PHP返回错误400的发布请求? Systatically错误请求

<?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; 
?> 

提到这个代码后运行,我得到错误400从服务器响应,指出该请求在语法上是错误的。请让我知道如果我做错了什么。

回答

1

您在设置Content-Length根据$data_string,但我认为您的意思是$data_post因为$data_string没有在任何地方定义。但是,尝试一起删除Content-Length条目,CURL将会计算并包含它。另外,在每个标题中的冒号后面加一个空格。

+0

我怎么可能这么粗心:(:(....感谢一堆杰克...... !!这绝对是让人搞砸了!荣誉!:) –

+1

请务必接受答案 – Palpatim

+0

@Palpatim完成! –