Jump to content

Why does my PHP socket strip XML tags?


hanwellchris

Recommended Posts

Hi

 

I have implemented a socket in PHP 5.1 which sends a command to a 3rd Party server. The server responds with some XML data. However the data I receive has had all of the XML tags removed, so that I get all of the data items concatenated together.

 

I have used some socket monitors, and I am, absolutely certain that the correct data is being sent buy the server, it is just not being received properly by PHP.

 

I am using fsockopen ( 127.0.0.1 ) and fgets, but I have also tried fread and it makes no difference. I still only ever get the data part of the XML string without the tags. I have searched the web and not found anything similar.

 

Does any one know what I might be doing wrong??

 

Al the Best

 

Chis

Link to comment
Share on other sites

I have not got to the point of parsing the xml. There is no xml to parse! It first fell over in my xml parsing, and that has let me back to the reception of the data through the socket. I read from the socket with the following function;

 

  function RLogWebServer( $Command, $Parameters )

  {

    $fp = fsockopen( "127.0.0.1", 3400, $errno, $errstr, 10 );

 

    if ( !$fp )

    {

      echo "<br />$errstr ( $errno )<br />\n";

      $Result = Array();

    }

    else

    {

      // Build the command (and parameter list) string

      $Out = $Command;

 

      if ( $Parameters != '' )

      {

        $Out .= ":".$Parameters;

      }

 

      $Out .= "\n";

     

      fputs( $fp, $Out, strlen( $Out ) );

      fflush( $fp );

 

      $In = '';

 

      while ( !feof( $fp ) )

      {

        $In .= fgets( $fp, 128 );

      }

 

      fclose( $fp );

     

      $Result = explode( "@@", trim($In) );

    }

 

    return $Result;

  }

 

I have put print statements at all points in this function and it is clear that the opriginal XML is nopt making it to the fgets function. The $In that I build the result in always contains the data part of the xml string.

 

If I send;

<Sensor><Name>Sensor1</Name><Type>7</Type><Status>0</Status></Sensor>

down the socket, I receive;

Sensor170

 

This is madness!!. I can see using socket tools (tcpspy) that I really am sending the full XML type string into the socket, but it just doesn't seem to be coming out the other end. It is as if some kind of filter is acting on the stream that I am not aware of. This same code worked 18months ago when I first tried it. I have just resurrected it for another project, and got stumped at the first hurdle. It seems as if it must be some PHP.INI setting, but I don't know what.

 

All the Best

 

Chris

 

Link to comment
Share on other sites

  • 1 year later...

I hope you've found the solution after all ;)

But if anyone encounters such problem in the future...

 

When you display your POST data try to use this function:

htmlspecialchars()

 

htmlspecialchars($_POST['data'])

 

Because the data is being sent properly, only when you display it, the php server ignores any tags in strings. This function changes "<" and some others into entities.

 

Sending:

$data = '<?xml version="1.0" encoding="UTF-8"?>';
$data .= '<test>';
$data .= '<gość>';
$data .= '<imię>Antek</imię>';
$data .= '<nazwisko>Jóźwiak</nazwisko>';
$data .= '<adres>Polna 12/4</adres>';
$data .= '<telefon>505444888</telefon>';
$data .= '</gość>';
$data .= '</test>';

 

You get

<?xml version=\"1.0\" encoding=\"UTF-8\"?><test><gość><imię>Antek</imię><nazwisko>Jóźwiak</nazwisko><adres>Polna 12/4</adres><telefon>505444888</telefon></gość></test>

 

Hope this would help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.