0

我遇到了一个愚蠢的问题:我需要获取一定数量的字节,然后关闭连接。我发现我可以从标头中提取“内容长度”,并使用该长度来确定在断开连接之前我应该​​获得多少字节。但是,看来我犯了一个错误。它只获得 40 个字节,而不是 2428(应该如此)。也许我没有正确使用 fgets() ?

这是代码:

private static function Send($URL, $Data) {
            $server = parse_url($URL, PHP_URL_HOST);
            $port = parse_url($URL, PHP_URL_PORT);

            // If the parsing the port information fails, we will assume it's on a default port.
            // As such, we'll set the port in the switch below.
            if($port == null) {
                switch(parse_url($URL, PHP_URL_SCHEME)) {
                    case "HTTP":
                        $port = 80;
                        break;
                    case "HTTPS":
                        $port = 443;
                        break;

                }
            }

            // Check if we are using a proxy (debug configuration typically).
            if(\HTTP\HTTPRequests::ProxyEnabled) {
                $server = \HTTP\HTTPRequests::ProxyServer;
                $port = \HTTP\HTTPRequests::ProxyPort;
            }



            // Open a connection to the server.
            $connection = fsockopen($server, $port, $errno, $errstr);
            if (!$connection) {
                die("OMG Ponies!");
            }
            echo "===========================================================<BR>";
            echo "Connection Open<BR>";
            echo "The Time is " . date("H:i:su", time()) . "<BR>";
            echo "Sending Request<BR>";

            fwrite($connection, $Data);

            echo "Request Sent.";
            echo "The Time is " . date("H:i:su", time()) . "<BR>";
            $responseheader = "";
            $responsebody = "";


            /*
            \HTTP\HTTPRequests::$start = NULL;
            \HTTP\HTTPRequests::$timeout = 10;

            // @todo: Rewrite this. Should keep checking for '/r/n/r/n', then check for a content length header. If found, keep grabbing bytes, then close. If not, then close immediately.
            while(!\HTTP\HTTPRequests::safe_feof($connection, \HTTP\HTTPRequests::$start) && (microtime(true) - \HTTP\HTTPRequests::$start) < \HTTP\HTTPRequests::$timeout)
            {
                $response .= fgets($connection);
            }
            */
            echo "Getting Response<BR>";
            echo "The Time is " . date("H:i:su", time()) . "<BR>";
            while(!feof($connection) && !(strlen(strstr($responseheader,"\r\n\r\n"))>0)) {
                $responseheader .= fgets($connection);
            }

            echo "The Header is fully received at " . date("H:i:su", time()) . "<BR>";
            echo "Header (raw):" . "<BR>";
            echo "/////////////////////////////////////////////<BR>";
            echo $responseheader . "<BR>";
            echo "/////////////////////////////////////////////<BR>";

            if((strlen(strstr($responseheader,"Content-Length:"))>0)) {
                $contentlength = ((int)(\Extract\Extract::GetContentLength($responseheader)));
                //for($i = 0; $i < $contentlength; $i++) {
                    $responsebody .= fgets($connection, $contentlength);
                //}
                echo "The Body is fully received at " . date("H:i:su", time()) . "<BR>";
                echo "Body (raw):" . "<BR>";
                echo "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}<BR>";
                echo $responsebody . "<BR>";
                echo "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}<BR>";
                echo "<B>!Content length is " . strlen($responsebody) . ". It should be " . $contentlength . "!</B><BR>";
            }

            echo "Response Received.<BR>";
            echo "The Time is " . date("H:i:su", time()) . "<BR>";
            fclose($connection);

            echo "Connection Closed.<BR>";
            echo "The Time is " . date("H:i:su", time()) . "<BR>";
            echo "Exiting Send() method.<BR>";
            echo "===========================================================<BR>";
            echo "<BR>";

            return $responseheader . $responsebody;
        }

这是输出:

===========================================================
Connection Open
The Time is 15:57:29000000
Sending Request
Request Sent.The Time is 15:57:29000000
Getting Response
The Time is 15:57:29000000
The Header is fully received at 15:57:29000000
Header (raw):
/////////////////////////////////////////////
HTTP/1.1 207 Multi-Status Date: Wed, 11 Apr 2012 19:57:18 GMT Server: Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.14 with Suhosin-Patch mod_ssl/2.2.8 OpenSSL/0.9.8g mod_wsgi/2.0 Python/2.5.2 mod_perl/2.0.3 Perl/v5.8.8 X-Powered-By: PHP/5.2.4-2ubuntu5.14 DAV: 1, 2, 3, access-control, calendar-access, calendar-schedule DAV: extended-mkcol, calendar-proxy, bind, addressbook, calendar-auto-schedule Content-Location: /davical/caldav.php/rwr26/home/ ETag: "8378ead7e628deafd91ec99dc180ad74" X-DAViCal-Version: DAViCal/0.9.9; DB/1.2.9 Content-Length: 2428 Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Content-Type: text/xml; charset="utf-8" 
/////////////////////////////////////////////
The Body is fully received at 15:57:29000000
Body (raw):
{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}

{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}
!Content length is 40. It should be 2428!
Response Received.
The Time is 15:57:29000000
Connection Closed.
The Time is 15:57:29000000
Exiting Send() method.
===========================================================

我为什么要这样做?显然,我遇到了某种超时错误,这使每个连接保持打开状态 30 秒(是的,整整 30 秒)。所以我写了这段代码来获取确切的字节数,然后关闭。任何帮助是极大的赞赏。

4

1 回答 1

0

我更改了内容行“$responsebody .= fgets($connection, $contentlength);” 到“$responsebody .= stream_get_line($connection, $contentlength);”。现在事情很糟糕,并且有效。

于 2012-04-21T00:02:16.583 回答