problem with fsockopen & fgets

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I'm stuck on a problem with getting data from a XML data stream.  This
stream is  large and trying to use fsockopen to get the stream down.

  I've tetsed the code by telneting into the machine/port but I am
getting some of the data but not all.  I don't know the stream size.

Code snippit below:

function txrx($server, $port, $tx){

  $rx = "";
  if ($server == "") {
    $rx = "Error - Missing Server";
  if ($port == "") {
    $rx = "Error - Missing Port";
  $fp = fsockopen($server, $port);
  if($fp) {
    fputs($fp, $tx);
    $rx .= fread($fp, 2048);
  return $rx;


Any suggestions ?

Can you please post to the group and send me an e-mail at

Colin Bell

Re: problem with fsockopen & fgets

Colin Bell wrote:

Quoted text here. Click to load it

Yes. you are only reading the first 2048 bytes.  Look at this example FROM THE
DOCS and compare it to yours... the problem should be obvious...

$fp = fsockopen("", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host:\r\n";
    $out .= "Connection: Close\r\n\r\n";

    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);

Quoted text here. Click to load it

Michael Austin.
Consultant - Available.
Donations welcomed.

Re: problem with fsockopen & fgets


On 08/20/2004 11:32 AM, Colin Bell wrote:
Quoted text here. Click to load it

If this is being sent to an HTTP server, you need to follow the HTTP
protocol. In that case you may want to try this HTTP client class that
provides a way to send files either by emulating POST form uploading or
just by submiting the XML file in the request body.


Manuel Lemos

PHP Classes - Free ready to use OOP components written in PHP /

PHP Reviews - Reviews of PHP books and other products /

Metastorage - Data object relational mapping layer generator

Site Timeline