tags:

views:

116

answers:

1

Hi,

I'm receiving an XML via php://input and after using simpleXML to break the elements down into variables and then what I want to do is append an array, or create an array of the variables every 30 seconds or so.

The reason is this script will be getting regular inputs, and rather than doing loads of mySQL updates or inserts, I assume it might be better for efficiency.

So, a couple of questions if anyone has a moment.

1) is there a way to check for a new input on php://input. 2) is there a better way to do this repeat check than sleep function? 3) how do I append/add to an array with these updating variables?

I haven't gone too far yet, so the code isn't useful but if you can forgive me simpleness:-

    function input() {

 $xml = new SimpleXMLElement($input);

 $session_id = $xml->session_id;
 $ip = $xml->ip;
 $browser = $xml->browser;

 store($session_id, $ip, $browser);
 }

function store() {
 $session_id = array();
 $ip = array();
 $browser = array();
}
A: 

If I understand you correctly, it seems that you are trying to use PHP for a long-running stateful program. I trust you know the following: PHP programs generally don't run for longer than a few miliseconds, at most a few seconds for the typical web application. Every time a resource is requested from the PHP handler, the parsing begins anew and there is no program state that remains from the previous execution. Being a stateless environment, it is up to you to maintain the state. For this reason, PHP is not made to handle input that changes with time, or to maintain a state.

This being said, the simplest way to append to an array is the follwing:

$myarray[] = "newvalue";

or

$myarray['newkey'] = "newvalue";

To process the stream:

while (!feof($handle)){ $data = fgets($handle, 4096); }
Antony Carthy
Good answer, I ended up doing this a different way... writing to a file instead and then event mySQL updating later.
WiseDonkey