php 如何阻止 cURL 使用 100 Continue?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14158675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I stop cURL from using 100 Continue?
提问by jmillar
So, long story short, I have an AJAX application that uses MVC Web API as the back end. The client however calls from a different domain and uses a PHP proxy file to get around cross-domain request issues.
所以,长话短说,我有一个 AJAX 应用程序,它使用 MVC Web API 作为后端。然而,客户端从不同的域调用并使用 PHP 代理文件来解决跨域请求问题。
However, using the PHP proxy, the Web API responds to certain requests with a 100 ContinueHTTP header and any requests that get this back take excessive time to complete (we're talking up to 2 mins or so) and can also return a non-valid response.
但是,使用 PHP 代理时,Web API 会使用100 ContinueHTTP 标头响应某些请求,并且返回此标头的任何请求都需要花费过多的时间才能完成(我们说的时间最长为 2 分钟左右),并且还可能返回无效的回复。
This appears to be a known issue with cURLand the workaround is commonly cited as inserting the below line to remove the expect: 100 header in the cURL request
这似乎是 cURL 的一个已知问题,解决方法通常被引用为插入以下行以删除 cURL 请求中的 expect: 100 标头
Unfortunately, the solution seems to be elusive for me:
不幸的是,解决方案对我来说似乎难以捉摸:
$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:') );
This code works but removes all the other headers (which isn't workable for me as I'm using HTTP basic auth headers to authenticate with the API).
You may also notice I tried adding the Expect:to the existing headers, but this didn't help me either.
此代码有效,但删除了所有其他标头(这对我来说不起作用,因为我使用 HTTP 基本身份验证标头对 API 进行身份验证)。您可能还注意到我尝试将Expect:加到现有标题中,但这对我也没有帮助。
How can I maintain the existing headers, but also prevent cURL from expecting a 100 continue?
如何维护现有的标头,同时防止 cURL 期望 100 继续?
回答by hakre
Using $headers_new[] = 'Expect:';does work unlessthe $headers_newarray contains a string that is 'Expect: 100-continue'. In this case you need to remove it from the array otherwise it will be expecting the 100 continue (logically).
使用$headers_new[] = 'Expect:';不工作,除非该$headers_new数组包含字符串是'Expect: 100-continue'。在这种情况下,您需要将它从数组中删除,否则它将期望 100 继续(逻辑上)。
Because in your code you use getallheaders()and you're not checking if it contains an Expect: 100-continueheader already so this probably is the case in your case.
因为在您使用的代码中getallheaders(),您没有检查它是否已经包含Expect: 100-continue标题,所以您的情况可能就是这种情况。
Here is a summary for the general situation (and the script that created it):
以下是一般情况的摘要(以及创建它的脚本):
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
Code:
代码:
<?php
$ch = curl_init('http://www.iana.org/domains/example/');
function curl_exec_continue($ch) {
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$continue = 0 === strpos($result, "HTTP/1.1 100 Continue\x0d\x0a\x0d\x0a");
echo "Continue: ", $continue ? 'Yes' : 'No', "\n";
return $result;
}
echo "GET request ..........................................: ", !curl_exec_continue($ch);
$header = array();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "GET request with empty header ........................: ", !curl_exec_continue($ch);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('hello'));
echo "POST request with empty header .......................: ", !curl_exec_continue($ch);
$header[] = 'Expect: 100-continue';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "POST request with expect continue explicitly set .....: ", !curl_exec_continue($ch);
$header[] = 'Expect:';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "POST request with expect (set to nothing) as well ....: ", !curl_exec_continue($ch);
unset($header[0]);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "POST request with expect continue from earlier removed: ", !curl_exec_continue($ch);
回答by Xenonite
Thanks for the script, Hakre. Since I needed this for HTTP PUT, I extended it a bit with the following results:
谢谢你的剧本,哈克。因为 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
Here's the script:
这是脚本:
<?php
$ch = curl_init('http://www.iana.org/domains/example/');
function curl_exec_continue($ch) {
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$continue = 0 === strpos($result, "HTTP/1.1 100 Continue\x0d\x0a\x0d\x0a");
echo "Continue: ", $continue ? 'Yes' : 'No', "\n";
return $result;
}
// --- GET
echo "GET request ..........................................: ", !curl_exec_continue($ch);
$header = array();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "GET request with empty header ........................: ", !curl_exec_continue($ch);
// --- POST
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('hello'));
echo "POST request with empty header .......................: ", !curl_exec_continue($ch);
$header[] = 'Expect: 100-continue';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "POST request with expect continue explicitly set .....: ", !curl_exec_continue($ch);
$header[] = 'Expect:';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "POST request with expect (set to nothing) as well ....: ", !curl_exec_continue($ch);
unset($header[0]);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "POST request with expect continue from earlier removed: ", !curl_exec_continue($ch);
// --- PUT
curl_setopt($ch, CURLOPT_PUT, TRUE);
$header = array();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "PUT request with empty header ........................: ", !curl_exec_continue($ch);
$header[] = 'Expect: 100-continue';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "PUT request with expect continue explicitly set ......: ", !curl_exec_continue($ch);
$header[] = 'Expect:';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "PUT request with expect (set to nothing) as well .....: ", !curl_exec_continue($ch);
unset($header[0]);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "PUT request with expect continue from earlier removed : ", !curl_exec_continue($ch);
// --- DELETE
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$header = array();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "DELETE request with empty header .....................: ", !curl_exec_continue($ch);
$header[] = 'Expect: 100-continue';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "DELETE request with expect continue explicitly set ...: ", !curl_exec_continue($ch);
$header[] = 'Expect:';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "DELETE request with expect (set to nothing) as well ..: ", !curl_exec_continue($ch);
unset($header[0]);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "DELETE request with expect continue from earlier removed : ", !curl_exec_continue($ch);
?>

