当cURL数据> 1024时,PHP HTTP POST失败

Bem*_*mmu 11 php post curl http

注意:最后的解决方案

如果我尝试执行超过1024个字符的HTTP POST,则会失败.为什么?这是一个最小的例子:

recipient.php:

<?php
if (strlen(file_get_contents('php://input')) > 1000
    || strlen($HTTP_RAW_POST_DATA) > 1000) {
 echo "This was a triumph.";
}
?>
Run Code Online (Sandbox Code Playgroud)

sender.php:

<?php
function try_to_post($char_count) {
 $url = 'http://gpx3quaa.joyent.us/test/recipient.php';
 $post_data = str_repeat('x', $char_count);
 $c = curl_init();
 curl_setopt_array($c,
                    array(  CURLOPT_URL => $url,
                            CURLOPT_HEADER => false,
                            CURLOPT_CONNECTTIMEOUT => 999,
                            CURLOPT_RETURNTRANSFER => true,
                            CURLOPT_POST => 1,
                            CURLOPT_POSTFIELDS => $post_data
                    )
 );
 $result = curl_exec($c);
 echo "{$result}\n";
 curl_close($c);
}

for ($i=1020;$i<1030;$i++) {
 echo "Trying {$i} - ";
 try_to_post($i);
}
?>
Run Code Online (Sandbox Code Playgroud)

输出:

Trying 1020 - This was a triumph.
Trying 1021 - This was a triumph.
Trying 1022 - This was a triumph.
Trying 1023 - This was a triumph.
Trying 1024 - This was a triumph.
Trying 1025 - 
Trying 1026 - 
Trying 1027 - 
Trying 1028 - 
Trying 1029 - 
Run Code Online (Sandbox Code Playgroud)

组态:

PHP Version 5.2.6
libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3 libidn/1.8
lighttpd-1.4.19
Run Code Online (Sandbox Code Playgroud)

为cURL添加以下选项:

curl_setopt($ch,CURLOPT_HTTPHEADER,array("Expect:"));
Run Code Online (Sandbox Code Playgroud)

原因似乎是任何超过1024个字符的POST都会导致发送"Expect:100-continue"HTTP标头,而Lighttpd 1.4.*不支持它.我找到了一张票:http://redmine.lighttpd.net/issues/show/1017

他们说它适用于1.5.

pil*_*lif 22

您可以通过设置显式请求标头来说服PHP的curl后端停止执行100-continue-thing:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
Run Code Online (Sandbox Code Playgroud)

通过这种方式,您可以发布请求,无论您想要多长时间,并且卷曲都不会执行双阶段发布.

我差不多两年前在博客上写过这篇文章.