如何阻止cURL使用100继续?

所以,简而言之,我有一个使用MVC Web API作为后端的AJAX应用程序。 然而,客户端从不同的域调用并使用PHP代理文件来解决跨域请求问题。

但是,使用PHP代理,Web API会使用100 Continue HTTP标头响应某些请求,并且任何获取此请求的请求都需要花费过多的时间才能完成(我们说的最多2分钟左右)并且还可以返回非 – 有效的回复。

这似乎是cURL的一个已知问题,解决方法通常被引用为插入以下行以删除cURL请求中的expect:100标头

不幸的是,解决方案对我来说似乎难以捉摸:

 $headers = getallheaders(); $headers_new = ""; foreach($headers as $title => $body) { $headers_new[] = $title.": ".$body; } //$headers_new[] = 'Expect:'; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_new); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:') ); 

此代码有效,但删除了所有其他标头(这对我来说是不可行的,因为我使用HTTP基本auth标头来通过API进行身份validation)。 您可能还注意到我尝试将Expect:添加到现有标题中,但这对我也没有帮助。

如何维护现有标头,还可以防止cURL期望100继续?

使用$headers_new[] = 'Expect:'; 除非 $headers_new数组包含一个'Expect: 100-continue'字符串, 否则它会工作。 在这种情况下,您需要将其从数组中删除,否则它将期望100继续(逻辑上)。

因为在你的代码中你使用getallheaders()并且你没有检查它是否包含Expect: 100-continue标题,所以在你的情况下可能就是这种情况。

以下是一般情况(以及创建它的脚本)的摘要:

 PHP Curl HTTP/1.1 100 Continue and CURLOPT_HTTPHEADER GET request ..........................................: Continue: No GET request with empty header ........................: Continue: No POST request with empty header .......................: Continue: Yes POST request with expect continue explicitly set .....: Continue: Yes POST request with expect (set to nothing) as well ....: Continue: Yes POST request with expect continue from earlier removed: Continue: No 

码:

  

谢谢你的剧本,哈克。 由于我需要这个用于HTTP PUT,我将其扩展了一下,结果如下:

 GET request ..........................................: Continue: No GET request with empty header ........................: Continue: No POST request with empty header .......................: Continue: Yes POST request with expect continue explicitly set .....: Continue: Yes POST request with expect (set to nothing) as well ....: Continue: Yes POST request with expect continue from earlier removed: Continue: No PUT request with empty header ........................: Continue: Yes PUT request with expect continue explicitly set ......: Continue: Yes PUT request with expect (set to nothing) as well .....: Continue: Yes PUT request with expect continue from earlier removed : Continue: No DELETE request with empty header .....................: Continue: Yes DELETE request with expect continue explicitly set ...: Continue: Yes DELETE request with expect (set to nothing) as well ..: Continue: Yes DELETE request with expect continue from earlier removed : Continue: No 

这是脚本: