php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26451 Output seems to be ignored
Submitted: 2003-11-28 13:37 UTC Modified: 2003-11-28 19:48 UTC
From: mflanagan at MJFlanagan dot com Assigned:
Status: Not a bug Package: Output Control
PHP Version: 4.3.3 OS: Windows 2000
Private report: No CVE-ID: None
 [2003-11-28 13:37 UTC] mflanagan at MJFlanagan dot com
Description:
------------
I have a server page that should be seinding output to a client php command-line script (not a browser).  The headers arrive at the client, but the resot of the output doesn't.  The same scripts work correctly under RH Linux (7.something) and Apache, same version of PHP.

Reproduce code:
---------------
LH_Svc.php: 
<?php
   $resp = "<response>\r\n";

   $respLen = strlen ($resp);
   header ("Content-Type: text/xml");
   header ("Content-Length: $respLen\n");
   echo ($resp);	// send back to caller
   exit;
?>

Client:
 <?php
   $server = "localhost";
   $url = "/earn/LH_Svc.php";
   $port = 80;

   // open socket
   $fp = fsockopen($server, $port);

   if (!$fp) {
      echo "Could not open socket";
   }
   else {
      $postData = "<test>";
      $dataLen = strlen ($postData);

      $postHeader = "POST $url HTTP/1.0\r\n";
      $postHeader .= "User-Agent: PHP-MJF Client\r\n";
      $postHeader .= "Content-Type: text/xml\r\n";
      $postHeader .= "Content-Length: $dataLen\n";
      $postHeader .= "\r\n";  // required extra blank line

      echo ($postHeader);
      echo ($postData);
      echo ("\n====================\n");

      fwrite ($fp, $postHeader);
      fwrite ($fp, $postData);

      $getResponse = fread ($fp, 14096);
      fclose ($fp);

      echo "Length of response is " . strlen ($getResponse) . "\r\n";
      echo $getResponse;

      echo "=====================\n";
   }
?>


Expected result:
----------------
D:\EarnApps>d:\php\php-4.3.3-win32\cli\php.exe post.cli     
 POST /earn/LH.php HTTP/1.0
User-Agent: PHP-MJF Client
Content-Type: text/xml
Content-Length: 6

<test>
====================
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Tue, 04 Nov 2003 18:30:44 GMT
X-Powered-By: PHP/4.3.3
Content-Type: text/xml
Content-Length: 10

<response>
=====================


Actual result:
--------------
D:\EarnApps>d:\php\php-4.3.3-win32\cli\php.exe post.cli     
 POST /earn/LH.php HTTP/1.0
User-Agent: PHP-MJF Client
Content-Type: text/xml
Content-Length: 6

<test>
====================
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Tue, 04 Nov 2003 18:30:44 GMT
X-Powered-By: PHP/4.3.3
Content-Type: text/xml
Content-Length: 10


=====================


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-11-28 13:52 UTC] iliaa@php.net
Change 

fread ($fp, 14096);

to

while (($line = fgets($fp, 1024))) {
$getResponse .= $line;
}
 [2003-11-28 18:35 UTC] mflanagan at MJFlanagan dot com
I made the change, and now see two things:

1.  it works, and

2.  the 'fread' version now works! (?)

If I reset IIS (iisreset), then the fread version does not work the first time I try it, but runs the second time.  The 'fgets' version always seems to run.

Thanks.

Michael
 [2003-11-28 19:48 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

You can change that fgets() to fread(). The bottom line is that you cannot rely on a single fread() to read all of the remaining data on a socket. You must do it in a loop until there is no more data to read or you've read the number of bytes you are looking for.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 10 18:01:33 2024 UTC