Jump to content

WEird Combination of fwrite and simplexml


cardio5643

Recommended Posts

<?php
// Change to absolute paths, relative paths have a chance of burning you if you're not careful
$inFile = '/etc/input.raw.txt';
$xmlFile = '/etc/input.xml';
$in = file_get_contents( 'php://input' );
file_put_contents( $inFile, $in ); // This is your raw input, run it through an XML validator and make sure it's valid
$xmlObj = new SimpleXMLElement( $in );
$xmlObj->asXML( $xmlFile ); // Should be the same XML as $inFile, but not necessarily same indenting
?>

 

I'm not even sure why you need to use SimpleXML at all though, unless you are interested in validating.

 

Seems you could just as easily do:

<?php
file_put_contents( '/etc/someXml.xml', file_get_contents( 'php://input' ) );
?>

 

I need to send raw, unorganised data to the server, which is running php scripts. I need to catch it with the php, organize it, store it, and send it back down to the client after its altered. I THOUGHT being that XML is simple enough as it is, php would have an XML parser. This is how I came across simplexml, there are others i think, but this is the one for my php version. upgrading is def a consideration, but im not asking the world here, i just wanted to get some xml, go traverse through it, and save it to a file. fml.

Link to comment
Share on other sites

I've used PHP's XMLWriter, XMLReader, and SimpleXML libraries.  They all work and some work better for some purposes than others.  SimpleXML is sufficient for what you're trying to do.

 

But you must remember that SimpleXML requires the received XML to be well-formed and syntactically valid.  If those conditions aren't met you'll just be in a world of hurt.

 

You also have to remember that elements and attributes in SimpleXML are always returned as SimpleXMLElement objects and not native data types.

Link to comment
Share on other sites

I've used PHP's XMLWriter, XMLReader, and SimpleXML libraries.  They all work and some work better for some purposes than others.  SimpleXML is sufficient for what you're trying to do.

 

But you must remember that SimpleXML requires the received XML to be well-formed and syntactically valid.  If those conditions aren't met you'll just be in a world of hurt.

 

You also have to remember that elements and attributes in SimpleXML are always returned as SimpleXMLElement objects and not native data types.

 

Understood, this is where the casting comes in. Such as fwrite(handle, (string)$simXml), correct?

 

And as for my raw xml, is

 

<root><number>1</number></root>

 

well-formed and syntactically valid ??

Link to comment
Share on other sites

Wow, this is pissing me off. Try this and open it in a browser. It will post the data to itself and hopefully provide some useful info:

<?php
   error_reporting(0);
   ini_set('display_errors','on');
   
   if($_SERVER['REQUEST_METHOD'] == 'POST'){
      $xmlFile = "../etc/someXMLfile.xml";
      $flashXML = simplexml_load_string($HTTP_RAW_POST_DATA);
      file_put_contents($xmlFile,print_r($flashXML,1));
      print "Done. Contents of file is:\n".file_get_contents($xmlFile);
      exit;
   }

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
   curl_setopt($ch, CURLOPT_POST, TRUE);
   curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
   curl_setopt($ch, CURLOPT_URL, 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
   curl_setopt($ch, CURLOPT_POSTFIELDS, '<root><number>1</number></root>');
   print "Output: <pre>".curl_exec($ch)."</pre>";
   print "Errors: ".curl_error($ch);
?>

Link to comment
Share on other sites

Wow, this is pissing me off. Try this and open it in a browser. It will post the data to itself and hopefully provide some useful info:

<?php
   error_reporting(0);
   ini_set('display_errors','on');
   
   if($_SERVER['REQUEST_METHOD'] == 'POST'){
      $xmlFile = "../etc/someXMLfile.xml";
      $flashXML = simplexml_load_string($HTTP_RAW_POST_DATA);
      file_put_contents($xmlFile,print_r($flashXML,1));
      print "Done. Contents of file is:\n".file_get_contents($xmlFile);
      exit;
   }

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
   curl_setopt($ch, CURLOPT_POST, TRUE);
   curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
   curl_setopt($ch, CURLOPT_URL, 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
   curl_setopt($ch, CURLOPT_POSTFIELDS, '<root><number>1</number></root>');
   print "Output: <pre>".curl_exec($ch)."</pre>";
   print "Errors: ".curl_error($ch);
?>

 

I'd send you a screen shot if i could. i created a new file, pasted that code exactly into it, went to my browser, called it, and got a completely blank and white screen..

Link to comment
Share on other sites

Change:

error_reporting(0);

to:

error_reporting(0xffffffff);

 

from a terminal, the following command

$: php test.php

 

returns

otice: Undefined index:  REQUEST_METHOD in /wfw/redir/test.php on line 5

 

Fatal error: Call to undefined function curl_init() in /wfw/redir/test.php on line 13

 

Link to comment
Share on other sites

If you are running it from the terminal:

 

-Enable Curl as @roopurt18 suggested

-Instead of:

   curl_setopt($ch, CURLOPT_URL, 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

use

   curl_setopt($ch, CURLOPT_URL, 'http://www.yourserver.com/path/to/file.php');

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.