PHP curl_exec返回由空格分隔的HTTP / 1.1 100 Continue和HTTP / 1.1 200 OK [英] PHP curl_exec returns both HTTP/1.1 100 Continue and HTTP/1.1 200 OK separated by space
问题描述
我使用cURL从PHP调用服务,如下所示:
I'm calling a service from PHP using cURL, like this:
$response = curl_exec($ch);
,请求/响应头如下所示:
and the request/response headers look something like this:
请求:
POST /item/save HTTP/1.1
Host: services.mydomain.com
Accept: */*
Content-Length: 429
Expect: 100-continue
Content-Type: multipart/form-data
回应:
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Fri, 06 Jul 2012 08:37:01 GMT
Server: Apache
Vary: Accept-Encoding,User-Agent
Content-Length: 256
Content-Type: application/json; charset=utf-8
后跟正文(json编码数据)。
followed by the body (json encoded data).
问题是,常见的是在遇到的第一个空行的响应中拆分头部和主体,除非在这种情况下,空行在 100
The problem is that the common thing is to split headers and body in the response by the first empty line encountered, except in this case, the empty line is after the 100 Continue
and therefore everything else gets pushed into the body–and that is not valid json anymore :-)
所以我的问题是这里:什么是有效的json?常见的方法来处理这个?
我有3个选项排列:
So my question is this: What's the common way to deal with this? I have 3 options lined up:
- 指定curl不应期望
code>? (How?)
- 指定curl应该只发送回上一个响应的标题吗? (How?)
- 手动检查
100继续
标头,并忽略它们及其以下空行? (在这种情况下,是否有其他类似的事情可能发生,我应该手动检查?)
- Specify that curl should not expect
100-continue
? (How?) - Specify that curl should only send back the headers of the last response? (How?)
- Manually check for
100 Continue
headers and disregard them and their following empty line? (In that case, are there other similar things that could happen, that I should manually check for?)
很明显,我相信人们已经绊倒了这个问题,并解决了很多次!
Unless I'm missing something obvious, I'm sure people have stumbled upon this and solved it many times!
推荐答案
我会选择#1。
你可以强制curl发送空的Expect头,添加:
I will opt for #1. You can force curl to send empty "Expect" header, by adding:
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect:"));
您的代码
手动检查,你应该定义你自己的头回调和可能写回调(查找CURLOPT_HEADERFUNCTION和CURLOPT_WRITEFUNCTION在 curl_setopt doc ),它只是简单地忽略所有HTTP / 1.1 100继续标题。
If you want check it manually, you should define your own header callback and maybe write callback (look for CURLOPT_HEADERFUNCTION and CURLOPT_WRITEFUNCTION in curl_setopt doc), which has simply to ignore all "HTTP/1.1 100 Continue" headers.
这篇关于PHP curl_exec返回由空格分隔的HTTP / 1.1 100 Continue和HTTP / 1.1 200 OK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!