Jump to content

WEird Combination of fwrite and simplexml


cardio5643

Recommended Posts

Hello, I am dying for help. I am trying with ajax to send raw xml data via javascript to the server side and catch it , with php. I can get the raw data and I can write a file. I CANNOT for the life of me, get this code to work. It can only be one or the other ,(simplexml or fwrite) if both are involved nothing works.

 

$flashRAW = file_get_contents("php://input");

$flashXML = simplexml_load_string($flashRAW);

$stringXML = $flashXML->number->{0};

$xmlFile = "../etc/someXMLfile.xml";

$xmlHandle = fopen($xmlFile, "w");

fwrite($xmlHandle, $stingXML);

fclose($xmlHandle);

 

Any help, ideas, suggestions, or work arounds would be greatly appreciated.

Link to comment
Share on other sites

   fwrite($xmlHandle, $stingXML);

should be

   fwrite($xmlHandle, $stringXML);

 

Yea just a minor typo while posting here, that is def. not the error in my php file. i work in a linux environment and when i 'php filename.php' the command line i get zero errors.

Link to comment
Share on other sites

try some debugging:

<?php
   $flashRAW = file_get_contents("php://input");
   print "Found ".strlen($flashRAW)." characters in php://input\n";
   $flashXML = simplexml_load_string($flashRAW)
      or die("Failed to load XML");
   $stringXML = $flashXML->number->{0};
   $xmlFile = "../etc/someXMLfile.xml";
   print "Writing \"{$stringXML}\" to file {$xmlFile}\n";
   file_put_contents($xmlFile,$stringXML)
      or die("Failed to write file");
?>

Link to comment
Share on other sites

try some debugging:

<?php
   $flashRAW = file_get_contents("php://input");
   print "Found ".strlen($flashRAW)." characters in php://input\n";
   $flashXML = simplexml_load_string($flashRAW)
      or die("Failed to load XML");
   $stringXML = $flashXML->number->{0};
   $xmlFile = "../etc/someXMLfile.xml";
   print "Writing \"{$stringXML}\" to file {$xmlFile}\n";
   file_put_contents($xmlFile,$stringXML)
      or die("Failed to write file");
?>

 

I would try this debugging and others also, but the php page is not what is loaded on the client side. the php is just to collect and save the data, after the reqXML.send(/*"<?xml version='1.0' encoding='UTF-8'?>" + */xmlBody);, the browser returns to the same html page. the reason i say this is because i do not kno how to see the php's output if the browswer is not landing on that page...help!?

Link to comment
Share on other sites

k, let's start simple then:

<?php
   $xmlFile = "../etc/someXMLfile.xml";
   $xmlHandle = fopen($xmlFile, "w");
   fwrite($xmlHandle, 'I found the file');
   fclose($xmlHandle);
?>

does that write something to the file?

Link to comment
Share on other sites

k, let's start simple then:

<?php
   $xmlFile = "../etc/someXMLfile.xml";
   $xmlHandle = fopen($xmlFile, "w");
   fwrite($xmlHandle, 'I found the file');
   fclose($xmlHandle);
?>

does that write something to the file?

 

Yes, file reads:

 

/wfw/etc# more someXMLfile.xml

I found the file

 

Link to comment
Share on other sites

k, now what is the output of:

<?php
   $xmlFile = "../etc/someXMLfile.xml";
   $xmlHandle = fopen($xmlFile, "w");
   $flashRAW = file_get_contents("php://input");
   $flashXML = simplexml_load_string($flashRAW);
   fwrite($xmlHandle, print_r($flashXML,true));
   fclose($xmlHandle);
?>

Link to comment
Share on other sites

k, now what is the output of:

<?php
   $xmlFile = "../etc/someXMLfile.xml";
   $xmlHandle = fopen($xmlFile, "w");
   $flashRAW = file_get_contents("php://input");
   $flashXML = simplexml_load_string($flashRAW);
   fwrite($xmlHandle, print_r($flashXML,true));
   fclose($xmlHandle);
?>

 

Thank you so much by the way for doing this.

if the file is not there, the script creates the file, but there is not any thing in the file.

Link to comment
Share on other sites

ok...that either means it's not receiving anything or the XML isn't parsing...let's step backwards..also, let's append instead of overwrite just in case we are getting multiple requests:

<?php
   $xmlFile = "../etc/someXMLfile.xml";
   $xmlHandle = fopen($xmlFile, "a+");
   $flashRAW = file_get_contents("php://input");
   $output  = "\n\n==================================\n";
   $output .= date('r')."\n------------------\n";
   $output .= $flashRAW;
   fwrite($xmlHandle, $output);
   fclose($xmlHandle);
?>

Link to comment
Share on other sites

ok...that either means it's not receiving anything or the XML isn't parsing...let's step backwards..also, let's append instead of overwrite just in case we are getting multiple requests:

<?php
   $xmlFile = "../etc/someXMLfile.xml";
   $xmlHandle = fopen($xmlFile, "a+");
   $flashRAW = file_get_contents("php://input");
   $output  = "\n\n==================================\n";
   $output .= date('r')."\n------------------\n";
   $output .= $flashRAW;
   fwrite($xmlHandle, $output);
   fclose($xmlHandle);
?>

 

Okay, now the file reads:

 

:/wfw/etc# more someXMLfile.xml

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

 

That xml is what I have been trying to send to the php server from client side javascript

 

Link to comment
Share on other sites

ug...how STUPID of me...the file functions don't cast them to strings...let me explain:

$flashXML->number->{0}

technically returns a SimpleXML Object. When you use echo/print it will cast it to a string and print it out, so you would get the number 1. But, the file functions don't do that automatically, so we have to cast it ourselves. your original code was OH so close (you also don't need the ->{0}):

<?php
   $flashRAW = file_get_contents("php://input");
   $flashXML = simplexml_load_string($flashRAW);
   $stringXML = $flashXML->number;
   $xmlFile = "../etc/someXMLfile.xml";
   $xmlHandle = fopen($xmlFile, "w");
   fwrite($xmlHandle, (string)$stringXML);
   fclose($xmlHandle);
?>

Link to comment
Share on other sites

ug...how STUPID of me...the file functions don't cast them to strings...let me explain:

$flashXML->number->{0}

technically returns a SimpleXML Object. When you use echo/print it will cast it to a string and print it out, so you would get the number 1. But, the file functions don't do that automatically, so we have to cast it ourselves. your original code was OH so close (you also don't need the ->{0}):

<?php
   $flashRAW = file_get_contents("php://input");
   $flashXML = simplexml_load_string($flashRAW);
   $stringXML = $flashXML->number;
   $xmlFile = "../etc/someXMLfile.xml";
   $xmlHandle = fopen($xmlFile, "w");
   fwrite($xmlHandle, (string)$stringXML);
   fclose($xmlHandle);
?>

 

 

wow you had me really really excited right there, i thought it was good to go. but now the script is not even touching the simplXMLfile. not even changing the modify date, or creating a new one if the file isnt there. i really don't understand

Link to comment
Share on other sites

huh....i don't see a difference...but what about:

<?php
   $xmlFile = "../etc/someXMLfile.xml";
   $xmlHandle = fopen($xmlFile, "w");
   $flashRAW = file_get_contents("php://input");
   $flashXML = simplexml_load_string($flashRAW);
   fwrite($xmlHandle, (string)$flashXML->number);
   fclose($xmlHandle);
?>

Link to comment
Share on other sites

huh....i don't see a difference...but what about:

<?php
   $xmlFile = "../etc/someXMLfile.xml";
   $xmlHandle = fopen($xmlFile, "w");
   $flashRAW = file_get_contents("php://input");
   $flashXML = simplexml_load_string($flashRAW);
   fwrite($xmlHandle, (string)$flashXML->number);
   fclose($xmlHandle);
?>

 

Okay so now it is touching the somxml file, if it is not there it will create it, but the actual file contains nothing.                      Its like if simplexml is involved, fwrite wont work...

Link to comment
Share on other sites

what happens when you do:

<?php
   $xmlFile = "../etc/someXMLfile.xml";
   $xmlHandle = fopen($xmlFile, "w");
   $flashRAW = '<root><number>1</number></root>';//file_get_contents("php://input");
   $flashXML = simplexml_load_string($flashRAW);
   fwrite($xmlHandle, (string)$flashXML->number);
   fclose($xmlHandle);
?>

Link to comment
Share on other sites

what happens when you do:

<?php
   $xmlFile = "../etc/someXMLfile.xml";
   $xmlHandle = fopen($xmlFile, "w");
   $flashRAW = '<root><number>1</number></root>';//file_get_contents("php://input");
   $flashXML = simplexml_load_string($flashRAW);
   fwrite($xmlHandle, (string)$flashXML->number);
   fclose($xmlHandle);
?>

 

Same thing, creates the file, but file has no contents. you are now doing exactly what i have been doing for the past 2 days. its great fun isnt it?

Link to comment
Share on other sites

so...you are telling me that this works:

<?php
   $xmlFile = "../etc/someXMLfile.xml";
   $xmlHandle = fopen($xmlFile, "w");
   fwrite($xmlHandle, 'test');
   fclose($xmlHandle);
?>

but this doesn't?

<?php
   $xmlFile = "../etc/someXMLfile.xml";
   $xmlHandle = fopen($xmlFile, "w");
   $flashXML = simplexml_load_string('<root><number>1</number></root>');
   fwrite($xmlHandle, 'test');
   fclose($xmlHandle);
?>

Link to comment
Share on other sites

so...you are telling me that this works:

<?php
   $xmlFile = "../etc/someXMLfile.xml";
   $xmlHandle = fopen($xmlFile, "w");
   fwrite($xmlHandle, 'test');
   fclose($xmlHandle);
?>

but this doesn't?

<?php
   $xmlFile = "../etc/someXMLfile.xml";
   $xmlHandle = fopen($xmlFile, "w");
   $flashXML = simplexml_load_string('<root><number>1</number></root>');
   fwrite($xmlHandle, 'test');
   fclose($xmlHandle);
?>

 

Yes, they both create the file if it is not there, but only the first bit of code actually writes something to the file...What should I do? Is there another way to do what I am trying to do??

Link to comment
Share on other sites

That is so strange. I can't imagine how that would affect that.

-Is this your server or with a hosting service?

-Do you have access to the PHP error logs, and is there anything in them?

-Is this the ACTUAL code you are using or an abbreviated version?

-What if you use different file functions:

<?php
   $xmlFile = "../etc/someXMLfile.xml";
   $flashXML = simplexml_load_string('<root><number>1</number></root>');
   file_put_contents($xmlFile,'test');
?>

Link to comment
Share on other sites

That is so strange. I can't imagine how that would affect that.

-Is this your server or with a hosting service?

-Do you have access to the PHP error logs, and is there anything in them?

-Is this the ACTUAL code you are using or an abbreviated version?

-What if you use different file functions:

<?php
   $xmlFile = "../etc/someXMLfile.xml";
   $flashXML = simplexml_load_string('<root><number>1</number></root>');
   file_put_contents($xmlFile,'test');
?>

 

It is my server, it is running off a linux box. I searched for as many logs as I could and found nothing having to do with this. And yes, this is the actual code, exactly what i am using.

 

That new file function was a no go either - didn't even create the file. I guess I am just stuck

Link to comment
Share on other sites

Have you tried upgrading your PHP the latest version? I haven't searched around, but there may be a bug in the particular version you are using.

 

Is this the extend of the XML you are going to receive? Or do you plan on expanding this and sending more? You can always just parse it as text to get the number.

Link to comment
Share on other sites

Have you tried upgrading your PHP the latest version? I haven't searched around, but there may be a bug in the particular version you are using.

 

Is this the extend of the XML you are going to receive? Or do you plan on expanding this and sending more? You can always just parse it as text to get the number.

 

No, this is just the simplest form of xml i could think of, the real xml will be the data held in a table containing roughly anywhere from 100 to 200 cells.

 

If the simplexml works on its own, as long as no file write is NOT contained elsewhere in the php file, is there ANY other way for me to see that i AM getting it, and successfully loading it into simplexml_load_string() ? Like if the browser were landing on this php page, instead of just calling its scripts, than i could just echo and debug like that, but since i am asynchronously sending the raw data up, I cannot land on that page...

Link to comment
Share on other sites

you should always be able to test it with a PHP script:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
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://www.yourserver.com/path/to/script.php');
curl_setopt($ch, CURLOPT_POSTFIELDS, '<root><number>1</number></root>');
print curl_exec($ch);
?>

 

edit: this should be a separate script, calling the script we have been discussing

Link to comment
Share on other sites

<?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' ) );
?>

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.