Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6096097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:53:14+00:00 2026-05-23T12:53:14+00:00

Background : I have to create a plain site that accepts incoming posted XML

  • 0

Background:
I have to create a plain site that accepts incoming posted XML and sends the XML to a server via a socket connection and in turn display the XML sent back from the server. Easy peasy.

Problem:
I had no problem utilising fsockopen() to connect to the server and sending the XML. Reading the XML from the server was a whole new problem. The normal

while (!feof($fp)) {    
    echo fgets($fp);
}

did not do the trick, since the server returns one XML string, and one XML string only (no length information, eof, eol, etc.). Thus it would wait until the timeout was hit, display the received XML and a timeout error. My problem is similar to this dinosaur.

In a nutshell I want to read XML on the socket and close it as soon as no more data is being sent (not wait for timeout). Setting the timeout to a low value was also not viable as the server response can vary between 2–30 seconds.

Solution:
After struggling for the entire afternoon I decided to share the following solution for this problem (do criticise).

$fp = fsockopen("123.456.789.1", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)";
} else {
    $wait = true;    
    $out = '<samplexml><item>something</item></samplexml>';

    // [1] disable blocking
    stream_set_blocking($fp, 0);
    fwrite($fp, $out);

    while (!feof($fp)) {
        $r = fgets($fp);
        echo $r;
        if (!strcmp($r, "")){                
            if (!$wait) {
                // [2] has recieved data on this socket before
                break;
            }
        } else {
            $wait = false;
        }
    }
    fclose($fp);
}

It turns out that my main problem was blocking. So first off, [1] I had to disable stream_set_blocking so that fgets() can continuously check if new data became available. If not disabled fgets() will get the XML from the server and then the loop will get stuck on the second try since it will wait until more data becomes available (which never will).

I know that as soon as we have read some data that we can immediatly close the connection if any empty fgets() are returned (thus we can still set the second parameter of fgets() if it should be necessary).

After using this site for months I finally got to post something on stackoverflow.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:53:14+00:00Added an answer on May 23, 2026 at 12:53 pm

    The snippet has several problems:

    • First, the comparison strcmp($r, "") doesn’t actually make sense. It works, but it doesn’t make sense. I don’t think fgets ever returns an empty string. If there is no more data available, it returns FALSE. The reason your comparison works is that FALSE is converted to an empty string. But it makes your code not clear.
    • Second, since you have the stream in non-blocking mode, data can be not available only temporarily. By breaking from the loop as soon as fgets returns false, you’re potentially reading only the first batch of data that was cached by the OS. You try to work around this by forcing a wait no data has been received yet, but it’s a brittle solution.
    • Finally, you busy loop waiting for the data. This is very resource intensive.

    The two last points can be solved by using stream_select. This way you can enforce a large timeout until the first packet is received and then use a small timeout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background I have a web app that will create an image from user input.
Background I have been asked by a client to create a picture of the
Background I am trying to create a copy of a business object I have
Background I have a massive db for a SharePoint site collection. It is 130GB
Background: I have a kubuntu laptop right now that I can't use wirelessly, i.e.
Background: we have an application that generates reports from HTML (that may or may
following background: i have a Table-valued Function that returns a Table that is a
I have a HTTP Live streaming server setup that serves segmented mp3 audio content
Background: I have a little video playing app with a UI inspired by the
Background: I have a module which declares a number of instance methods module UsefulThings

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.