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

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 preLaunchTask"C/C++: aarch64- apple-darwin22-g++-14 生成活动 文件”已终止,退出代码为-1。
  • ¥18 关于#贝叶斯概率#的问题:这篇文章中利用em算法求出了对数似然值作为概率表参数,然后进行概率表计算,这个概率表是怎样计算的呀
  • ¥20 C#上传XML格式数据
  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥100 单片机hardfaulr
  • ¥20 手机截图相片分辨率降低一半
  • ¥50 求一段sql语句,遇到小难题了,可以50米解决
  • ¥15 速求,对多种商品的购买力优化问题(用遗传算法、枚举法、粒子群算法、模拟退火算法等方法求解)
  • ¥100 速求!商品购买力最优化问题(用遗传算法求解,给出python代码)
  • ¥15 虚拟机检测,可以是封装好的DLL,可付费