cardio5643 Posted July 30, 2009 Author Share Posted July 30, 2009 <?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 https://forums.phpfreaks.com/topic/167807-weird-combination-of-fwrite-and-simplexml/page/2/#findComment-886938 Share on other sites More sharing options...
roopurt18 Posted July 30, 2009 Share Posted July 30, 2009 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 https://forums.phpfreaks.com/topic/167807-weird-combination-of-fwrite-and-simplexml/page/2/#findComment-886952 Share on other sites More sharing options...
cardio5643 Posted July 30, 2009 Author Share Posted July 30, 2009 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 https://forums.phpfreaks.com/topic/167807-weird-combination-of-fwrite-and-simplexml/page/2/#findComment-886958 Share on other sites More sharing options...
rhodesa Posted July 30, 2009 Share Posted July 30, 2009 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 https://forums.phpfreaks.com/topic/167807-weird-combination-of-fwrite-and-simplexml/page/2/#findComment-886961 Share on other sites More sharing options...
roopurt18 Posted July 30, 2009 Share Posted July 30, 2009 Well I guess it may be, though it should have the xml opening tag and version information as well. You are right about the casting. SimpleXML also has an asXML() method you might look into. Link to comment https://forums.phpfreaks.com/topic/167807-weird-combination-of-fwrite-and-simplexml/page/2/#findComment-886962 Share on other sites More sharing options...
cardio5643 Posted July 30, 2009 Author Share Posted July 30, 2009 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 https://forums.phpfreaks.com/topic/167807-weird-combination-of-fwrite-and-simplexml/page/2/#findComment-886964 Share on other sites More sharing options...
roopurt18 Posted July 30, 2009 Share Posted July 30, 2009 Change: error_reporting(0); to: error_reporting(0xffffffff); Link to comment https://forums.phpfreaks.com/topic/167807-weird-combination-of-fwrite-and-simplexml/page/2/#findComment-886967 Share on other sites More sharing options...
cardio5643 Posted July 30, 2009 Author Share Posted July 30, 2009 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 https://forums.phpfreaks.com/topic/167807-weird-combination-of-fwrite-and-simplexml/page/2/#findComment-886970 Share on other sites More sharing options...
roopurt18 Posted July 30, 2009 Share Posted July 30, 2009 Open your php.ini and enable the php_curl extension. Make sure you restart your web server too. Link to comment https://forums.phpfreaks.com/topic/167807-weird-combination-of-fwrite-and-simplexml/page/2/#findComment-886971 Share on other sites More sharing options...
rhodesa Posted July 30, 2009 Share Posted July 30, 2009 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 https://forums.phpfreaks.com/topic/167807-weird-combination-of-fwrite-and-simplexml/page/2/#findComment-887015 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.