找到你要的答案

Q:PHP Curl giving Bad Request - Invalid Header [duplicate]

Q:PHP CURL提供错误的请求标头无效[复制]

This question already has an answer here:

  1. When i am trying to hit the curl with xml headers as well as post fields. I am getting error as bad request. I don't know what's going wrong, can any one help with this?

  2. Mobile field data passed in header is triple des converted


$url="www.test.com/api";
$xml = '<?xml version="1.0" encoding="UTF-8"?><a><b>hai</b></a>';

$xml_header = trim('<?xml version="1.0" encoding="UTF-8"?><mobile>4xKReHFU60Ova0aqHxZldg==</mobile>
')  ;

$headers = array(
"Content-type: text/xml",
"Content-length: " .strlen($xml_header),
"Connection: close",
);
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// set url 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//return the transfer as a string 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("xml"=>$xml));
// Check if any error occurred
if(!curl_errno($ch))
{
$info = curl_getinfo($ch);

echo "<pre>";var_dump($info);
echo 'error:' . curl_error($ch);
//echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
}                                   
$result = curl_exec($ch);
echo "<pre>";print_r($result);exit;

这个问题在这里已经有了答案:

  1. 当我试图用XML头和POST字段来击中卷曲时。我得到错误作为坏的要求。我不知道到底出了什么问题,有人能帮忙吗?

  2. 报头中传递的移动字段数据是三元组转换的


$url="www.test.com/api";
$xml = '<?xml version="1.0" encoding="UTF-8"?><a><b>hai</b></a>';

$xml_header = trim('<?xml version="1.0" encoding="UTF-8"?><mobile>4xKReHFU60Ova0aqHxZldg==</mobile>
')  ;

$headers = array(
"Content-type: text/xml",
"Content-length: " .strlen($xml_header),
"Connection: close",
);
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// set url 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//return the transfer as a string 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("xml"=>$xml));
// Check if any error occurred
if(!curl_errno($ch))
{
$info = curl_getinfo($ch);

echo "<pre>";var_dump($info);
echo 'error:' . curl_error($ch);
//echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
}                                   
$result = curl_exec($ch);
echo "<pre>";print_r($result);exit;
answer1: 回答1:

Bad Request is normally send from the server you interact with to tell you that there was a serious problem with the request, most often a malformed HTTP request up to that point that the remote server does not know at all what to do with it.

That is to say: You hit the jackpot of breaking it.

Remove bit after bit from your request until you get a more specific error message that can guide you more precisely when adding things again to not run into the same error again.

This is only general advice. The problem I could spot more concretely has been asked and answered already and I therefore closed it against a duplicate that should remind you, that having curl dealing with the content-length is most often easier as when you try to deal yourself with it (no pun intended).

错误的请求通常是从与你互动告诉你,有一个要求的严重问题的服务器发送,最常见的一种畸形的HTTP请求到这一点,远程服务器根本不知道该做什么。

也就是说:你打破了。

从你的请求中移除位之后,直到你得到一个更具体的错误信息,它可以更精确地引导你,当再次添加东西,而不是再次遇到同样的错误。

这只是一般性的建议。这个问题,我可以更具体地已被要求和回答已经和我因此关闭了一个副本,它应该提醒你,有卷曲处理的内容长度是最容易的,当你试图处理自己(没有双关意图)。

php  xml  curl