duanqiu2064 2012-07-06 09:14
浏览 515
已采纳

PHP curl_exec返回HTTP / 1.1 100 Continue和HTTP / 1.1 200 OK,以空格分隔

I'm calling a service from PHP using cURL, like this:

$response = curl_exec($ch);

and the request/response headers look something like this:

Request:

POST /item/save HTTP/1.1
Host: services.mydomain.com
Accept: */*
Content-Length: 429
Expect: 100-continue
Content-Type: multipart/form-data

Response:

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

followed by the body (json encoded data).

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 :-)

So my question is this: What's the common way to deal with this? I have 3 options lined up:

  1. Specify that curl should not expect 100-continue? (How?)
  2. Specify that curl should only send back the headers of the last response? (How?)
  3. 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!

  • 写回答

4条回答 默认 最新

  • dsgdg46465 2012-07-06 09:17
    关注

    I will opt for #1. You can force curl to send empty "Expect" header, by adding:

    curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect:"));
    

    to your code

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式