做网盘,上传文件到服务器时(服务器使用squid做代理),请求失败。返回了下面的错误提示:

161735_iXGi_197193.jpg

百思不得其解,查看PHP文档时,笔记中发现如下说明:

Sending a post file upload across a squid proxy, the request was
rejected by the proxy. In the error page returned it provided among
other possible causes:"Expect:" feature is being asked from a
HTTP/one.zero.

Solution: Add the option
<?php
curl_setopt($cl,CURLOPT_HTTPHEADER,array("Expect:")); ?>.
This will
remove the expect http header.

按照方案去掉“Expect:”的header头,问题解决!

更新:在鸟哥的博客看到了原理说明:

在使用curl做POST的时候, 当要POST的数据大于1024字节的时候, curl并不会直接就发起POST请求, 而是会分为俩步:

  1. 发送一个请求, 包含一个Expect:100-continue, 询问Server使用愿意接受数据
  2. 接收到Server返回的100-continue应答以后, 才把数据POST给Server

这是libcurl的行为.

具体的RFC相关描述: http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3

于是,这样就有了一个问题, 并不是所有的Server都会正确应答100-continue, 比如lighttpd, 就会返回417 “Expectation Failed”, 则会造成逻辑出错,,

要解决的办法也挺容易:

 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
 // Disable Expect: header (lighttpd does not support it)

标签: squid, curl, 网盘的坑

添加新评论