Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
错误:php脚本中的分块编码不完整_Php_Http - Fatal编程技术网

错误:php脚本中的分块编码不完整

错误:php脚本中的分块编码不完整,php,http,Php,Http,尝试在新的共享服务器帐户上运行一些php脚本时,脚本会不断挂起并超时,而不会出现错误消息。只有在web开发者控制台启动的情况下,在Chrome中运行脚本时,我才得到一点提示:“net::ERR_complete_CHUNKED_ENCODING”就是它所说的 Fiddler2则更为具体: Fiddler.Network.ProtocolViolation-[#165]传输编码:分块响应没有以正确的零大小分块终止 ……和: Fiddler.Network.ProtocolViolation-[#1

尝试在新的共享服务器帐户上运行一些php脚本时,脚本会不断挂起并超时,而不会出现错误消息。只有在web开发者控制台启动的情况下,在Chrome中运行脚本时,我才得到一点提示:“net::ERR_complete_CHUNKED_ENCODING”就是它所说的

Fiddler2则更为具体:

Fiddler.Network.ProtocolViolation-[#165]传输编码:分块响应没有以正确的零大小分块终止

……和:

Fiddler.Network.ProtocolViolation-[#165][HTTPLint#M012]HTTP分块响应体不完整;很可能缺少最后的0大小块

我制作了一个测试脚本来演示这个问题。它所做的只是在随机的20-60秒内使用sleep(),然后显示一个随机字符串。默认情况下是十次迭代

但是这个托管提供商的服务台坚持认为这与php环境中的60秒超时有关,我知道这是胡说八道。所以我编写了第二个测试脚本,它使用Ajax连续多次调用另一个脚本。同样,它非常简单,只需在40到55秒之间随机设置一个sleep(),然后生成并显示一个短的随机字符串

所以执行的时间永远不会超过60秒。但它仍然失败,尽管Chrome控制台中xhr调用的“net::ERR_EMPTY_RESPONSE”而不是“net::ERR_complete_CHUNKED_ENCODING”

当我向他们展示这一点时,他们实际上承认存在一个问题,这感觉像是一场胜利。但这是短暂的。第二天,他们回来说,很抱歉,他们不知道是什么原因造成的,他们无能为力,我有没有考虑升级到vps

我有点痴迷了。我想知道这到底是什么原因

我编写的第一个测试脚本的代码:

<?php

set_time_limit(3600);
error_reporting(E_ALL); 
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);


function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[mt_rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

isset($_REQUEST["limit"]) ? $limit = $_REQUEST["limit"] : $limit = 10;



echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">';

echo "<title>Test Script</title></head><body>";

//ob_start ();
echo "<br><br>A random delay between 20 and 60 seconds will be generated, then a randomly generated string will be displayed<br>The default limit on iterations is ten<br>When the script terminates normally, the phrase \"test complete\" will be output at the bottom<br><br>";
 flush();


for ($i = 0; $i < $limit; $i++) {
  $delay = mt_rand (20, 60);
  echo "<br><br><br>iteration ".($i+1)." - script will now sleep for $delay seconds";
    flush();
  sleep ($delay);
  echo "<br><br>Here is a random string:<br>";
  $length = mt_rand (50, 100);
  echo generateRandomString($length);
    flush();
} 


echo '<br><br>...test complete</body></html>';
ob_end_flush();
?>

您可以转到此页面:亲自查看在该服务器上运行时会发生什么

第二个测试脚本的代码:

<?php
set_time_limit(600);
error_reporting(E_ALL); 
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

isset($_REQUEST["limit"]) ? $limit = $_REQUEST["limit"] : $limit = 15;
isset($_REQUEST["longorshort"]) ? $longorshort = $_REQUEST["longorshort"] : $longorshort = "long";
$start = 1;

echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">';

echo "<title>Test Script 2</title></head><body>";
echo '<script src="test_script_js7.js"></script>';

echo "
  <br>Clicking the button below will start the script running
  <br>The script called via XMLHttpRequest will generate a random delay between 40 and 55 seconds (5-10 seconds if \"longorshort\" is set to \"short\" in the url string), then a randomly generated string will be displayed in the table on the bottom
  <br>The default limit on iterations is fifteen
  <br>When the process terminates normally, the phrase \"test complete\" will be output in the \"Main Info\" cell
  <br><b>The script called via XMLHttpRequest will <u>never take more than 60 seconds</u> to complete processing</b>

";
//echo "<img src=http://nzbstar.info/download_batch.png onclick=\"getter($limit, '$start', '$longorshort')\"><br>";
echo "<br><img src=start.jpg onclick=\"getter_outer($limit, '$start', '$longorshort')\"><br><a href=\"javascript:master_switch();\">click here to abort</a>  ";

echo "<table border=1><tr>";
echo "<td valign=top><div id=main_info>Main Info:</div></td>";
echo "<td valign=top><div id=iteration>Iterations:</div></td>";
echo "<td valign=top><div id=message>Messages:</div></td>";
echo "</tr></table>";

echo "<table border=1 style=table-layout:fixed;><tr>";
for ($i = 1; $i <= $limit; $i++) {
  echo "<td valign=top><div class=getter id=post_$i><i>Result $i</i></div></td>";
  if ( ($i%5 == 0) ) {echo "</tr><tr>";}
}
echo "</tr></table>";




echo '</body></html>';
?>

…javascript:

var master_off = false;
var getter_running_now = false;

function getter_outer(limit, i, longorshort){
  if (getter_running_now) {alert ("Script is already running!"); return;}
  getter_running_now = true;

  if (master_off) {document.getElementById("main_info").innerHTML += "<br>master switch off, aborting!"; return;} 
  if (i >= limit) {document.getElementById("main_info").innerHTML += "<br>Got to the next getter when we shouldn't have, there's a bug!"; return;}
  getter(limit, i, longorshort);
}

function getter(limit, i, longorshort){  
  if (master_off) {document.getElementById("main_info").innerHTML += "<br>master switch off, aborting!"; return;} 
  if (i >= limit) {document.getElementById("main_info").innerHTML += "<br>Got to the next getter when we shouldn't have, there's a bug!"; return;}

  var post_number_element = "post_" + i;

  var batch_result_script = "test_script_2a.php?longorshort=" + longorshort;

  document.getElementById("iteration").innerHTML = "Iterations:<br>Doing iteration: " + i;
  document.getElementById(post_number_element).innerHTML = "doing this one...<br>";
  var xmlhttp=new XMLHttpRequest();
  xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4){
      if (xmlhttp.status==200){
        var result = "<b>result for " + post_number_element + " is:</b><br>" + xmlhttp.responseText + "<br><span style=color:green;>Success!</span>";
        document.getElementById(post_number_element).innerHTML = result;
        i++;
        if (i >= limit) {
          document.getElementById("main_info").innerHTML += "<br><span style=color:green;>Test complete!</span>";
        }
        else {getter(limit, i, longorshort);}
      }
      else {document.getElementById("message").innerHTML += "<br> - <span style=color:red;>http return status for iteration " + i + " was " + xmlhttp.status + "</span>";}
    }
  }
  xmlhttp.open("GET",batch_result_script,true);
  xmlhttp.send();
}



function master_switch(){
  master_off = true;
  document.getElementById("main_info").innerHTML += "<br><span style=color:red;>Master Switch Off!</span>";
var master\u off=false;
var getter_running_now=false;
函数getter_外部(极限、i、长或短){
if(getter_running_now){alert(“脚本已在运行!”);return;}
getter_running_now=true;
if(master_off){document.getElementById(“main_info”).innerHTML+=“
主控开关关闭,中止!”;return;} 如果(i>=limit){document.getElementById(“main_info”).innerHTML+=“
在我们不应该的时候到达下一个getter,则会出现错误!”;return;} getter(极限、i、长或短); } 函数getter(limit,i,longorshort){ if(master_off){document.getElementById(“main_info”).innerHTML+=“
主控开关关闭,中止!”;return;} 如果(i>=limit){document.getElementById(“main_info”).innerHTML+=“
在我们不应该的时候到达下一个getter,则会出现错误!”;return;} var post\u number\u element=“post\u”+i; var batch_result_script=“test_script_2a.php?longorshort=“+longorshort; document.getElementById(“迭代”).innerHTML=“迭代:
进行迭代:”+i; document.getElementById(post_number_元素).innerHTML=“正在做这件事…”; var xmlhttp=new XMLHttpRequest(); xmlhttp.onreadystatechange=函数(){ if(xmlhttp.readyState==4){ if(xmlhttp.status==200){ var result=“post_number_element+的结果是:
”+xmlhttp.responseText+“
成功!”; getElementById(post\u number\u元素).innerHTML=result; i++; 如果(i>=限制){ document.getElementById(“main_info”).innerHTML+=“
测试完成!”; } else{getter(limit,i,longorshort);} } else{document.getElementById(“message”).innerHTML+=“
-http返回迭代“+i+”的状态为“+xmlhttp.status+”;} } } open(“GET”,批处理结果脚本,true); xmlhttp.send(); } 功能主控_开关(){ master_off=真; document.getElementById(“主信息”).innerHTML+=“
主开关关闭!”;
}

…以及它通过ajax调用的脚本:

 <?php
set_time_limit(600);
error_reporting(E_ALL); 
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);


function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[mt_rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

if ( isset($_REQUEST["longorshort"]) && $_REQUEST["longorshort"] == "short" ) {$delay = mt_rand (5, 10);}
else {$delay = mt_rand (40, 55);}


sleep ($delay);
echo "<br>Here is a random string:<br>";
$length = mt_rand (5, 10);
echo generateRandomString($length);


?>

走到这里:看看它在起作用。与第一个脚本不同,它生成的错误是“空响应”。但我认为这两者是相关的

这两个脚本在另一个共享服务器和我可以访问的带有LAMP的vps上都可以正常工作。它们还可以在此服务器上工作一段时间,直到出现故障。第一个测试脚本通常在死之前经过几次迭代。如果“longorshort”设置为“short”,则第二个选项(使用Ajax)有时会一直运行到完成

此外,第一个测试脚本在命令行中工作得非常完美。当然,第二种方法在那种环境下不起作用

服务器在Linux下运行PHP版本5.4.44和Apache版本2.4.16

谷歌不是我的朋友。我发布到Stackoverflow,得到了5(五)个视图,零响应


这里有人能至少给我一点线索吗?否则,请给我指出Stackoverflow的另一个替代方案,在那里他们可能会回答我的问题?

找到了你的wireshark信息的答案,就像所有这样写的延迟一样,58秒的延迟困扰着我

当Internet Explorer与Web服务器建立持久HTTP连接时(通过使用connection:Keep-Alive标头),Internet Explorer将重用用于接收初始请求的相同TCP/IP套接字,直到套接字空闲一分钟连接空闲一分钟后,Internet Explorer将重置连接。新的TCP/IP套接字用于接收其他请求。您可能需要更改Internet Explorer中的HTTP KeepAliveTimeout值

这让我认为其他浏览器必须遵循该标准,因此在脚本运行时必须在线发送一些内容,以防止关闭连接,因为您不能要求客户这样做