未设置 PHP cURL 内容类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19636367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP cURL Content-Type is not set
提问by Mithrand1r
I have a simple web service that I would like to connect with. In order to post some XML
, which will be properly proceeded on the web service side, I need to prepare a proper request. I am using cURL
for this like that:
我有一个想要连接的简单 Web 服务。为了发布一些XML
将在 Web 服务端正确处理的 ,我需要准备一个正确的请求。我正在使用cURL
这样的:
try {
$ch = curl_init();
if (FALSE === $ch)
throw new Exception('failed to initialize');
curl_setopt($ch, CURLOPT_URL,"192.168.1.37");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/xml',
'Connection: Keep-Alive'
));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_PROXY, '');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect: "));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS,$xml);
$request= curl_getinfo($ch);
var_dump($request);
$content = curl_exec($ch);
if (FALSE === $content)
throw new Exception(curl_error($ch), curl_errno($ch));
} catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),E_USER_ERROR);
}
Everything should be ok. First I am displaying the request body from curl_getinfo($ch);
and then i am showing the response from $content
variable.
一切都应该没问题。首先我显示请求正文curl_getinfo($ch);
,然后我显示来自$content
变量的响应。
Only restriction for request to be sent to the server is Content-Type
header - it MUST be application/xml
otherwise server will respond with an error ( It is not my idea and I can't do anything about it)
将请求发送到服务器的唯一限制是Content-Type
标头 -application/xml
否则服务器将响应错误(这不是我的想法,我无能为力)
Here is output request:
这是输出请求:
array(21) { ["url"]=> string(68) "192.168.1.37" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(0) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0) ["namelookup_time"]=> float(0) ["connect_time"]=> float(0) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } }
array(21) { ["url"]=> string(68) "192.168.1.37" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(0) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0) ["namelookup_time"]=> float(0) ["connect_time"]=> float(0) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } }
and response is HTTP 400 Bad Request
和回应是 HTTP 400 Bad Request
What I can see from that is ["content_type"]=> NULL
but I set this variable in my PHP code...
我可以看到的是,["content_type"]=> NULL
但我在我的 PHP 代码中设置了这个变量......
Can anyone tell me what I am missing?
谁能告诉我我错过了什么?
回答by Legionar
You are setting HTTP_HEADER twice:
您正在设置 HTTP_HEADER 两次:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/xml',
'Connection: Keep-Alive'
));
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect: "));
I guess that is the problem. The second time you set it, you delete the first setting.
我想这就是问题所在。第二次设置时,删除第一个设置。