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

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi