PHP不断地从服务器读取


php reading from the server continuously

我正在尝试从服务器连续读取。我使用fopensock()方法。每个连接只读取一次数据的问题。每行长度为350位。

这是我的代码:

 <?php
$fp = fsockopen("maddress",myport, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />'n";
} else {
    $out = "GET / HTTP/1.1'r'n";
    $out .= "Host: myaddress'r'n";
    $out .= "Connection: Close'r'n'r'n";
    fwrite($fp, $out);
 while ( ($line = fgets($fp)) !== false) {
   echo "$line<br>";

   };
    fclose($fp);
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
            <script type="text/javascript">
            window.onload=refresh;
            function refresh(){
                getTarget();
                setTimeout("refresh();",5000);
            }
  function getTarget(){

//1-creating object for the request
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
    if (window.ActiveXObject) {
                    try {
    xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
                    } catch(e) {
    xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
                    }
}
//2- excute response when it is ready
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
document.getElementById('targetText').innerText==xmlhttp.responseText;
    setTimeout("getTarget();", 5000);
           }
}   
//3- send request to the server
xmlhttp.open("GET","newEmptyPHPWebPage2.php",true);
//4- setting header for the request
xmlhttp.send(null);
}

</script>        

    </head>
    <body>

        <label id="targetText"></label>
    </body>
</html>

谢谢你的关心!

while ( ($line = fgets($fp)) !== false) {
   echo "$line<br>";
}

这会让你进入一个连续的循环,不断地阅读。