Jump to content

Error parsing xml data into array


rushman005

Recommended Posts

Like I said, you cannot use this script for regular output. Not without making changes.

 

Changes such as

1. Make the script quit after the second XML document or a suitable timeout. Then you can use output, such as what device triggered if any.

2. Make another page call this script using AJAX. It waits for the script to end (and to receive the output).

3. When that AJAX call ends the Javascript code used to make it can check the output to see if a device triggered or not. If so then it can create the popup.

4. Regardless, it starts another AJAX request to resume waiting for a detection event.

 

Like i said i have understood this now ad i will work towards getting that done, but there is one major issue now. If i run the code and it output two XML , and stops if i tried to load it again it will not output anything again, it will just connect and keep rolling, so even at this if i call the page with AJAX for information again it will not output anything.. Please what could possibly be the problem. If this problem is resolved i will find how i can make calls at an interval with AJAX

Thank you

Link to comment
Share on other sites

  • Replies 68
  • Created
  • Last Reply

echo '1';
If you do this then the JSON later will break. Don't do anything for the handshake document. Leave it alone. Ignore it.

 

echo json_encode([
"key" => (string)$xml->DeviceDetectionRecord->Detection->DetectionEvent
]);
Then the JSON will be like {"key":"???"} which you can get in Javascript. That's fine.

 

if i tried to load it again it will not output anything again, it will just connect and keep rolling

As long as the script is executing the second time correctly then it should try to connect again, just like it did before. Are you saying the detection XML isn't being sent when an event occurs? What happens if you just... wait?

 

What's the Javascript/AJAX on the frontend page that you're using to do the first and second load?

Link to comment
Share on other sites

 

echo '1';
If you do this then the JSON later will break. Don't do anything for the handshake document. Leave it alone. Ignore it.

 

echo json_encode([
"key" => (string)$xml->DeviceDetectionRecord->Detection->DetectionEvent
]);
Then the JSON will be like {"key":"???"} which you can get in Javascript. That's fine.

 

As long as the script is executing the second time correctly then it should try to connect again, just like it did before. Are you saying the detection XML isn't being sent when an event occurs? What happens if you just... wait?

 

What's the Javascript/AJAX on the frontend page that you're using to do the first and second load?

 

It still not working when i ran the script.

Even if i wait, it will not happen.

i just want to be sure it echoing out something, thats why i echoed

 echo "1"; 
in the handshake

 

but now when i ran this updated code, i'm receiving error

 

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
UNDER JSON

 

<br />
<b>Warning</b>:  socket_read(): unable to read from socket [10060]: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
 in <b>C:\xampp\htdocs\integration\rework.php</b> on line <b>29</b><br />
<br />
<b>Warning</b>:  Socket error: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
 in <b>C:\xampp\htdocs\integration\rework.php</b> on line <b>37</b><br />

 

UNDER RAW DATA..

 

I just want the code you wrote to work fine by collecting XML data and displaying content of the tags, the i will know how to fix it with the AJAX

I'm not using any AJAX/Javascript to do anything yet sir

 

Thank you

Link to comment
Share on other sites

As the error messages say, there's a connection timeout. You have to find out why.

 

Honestly have being trying to troubleshoot this, still the same result and if i use with the existing monitoring software it will connect instantly.

 

I'll ask again: What's the Javascript/AJAX on the frontend page that you're using to do the first and second load?

This is how i echo the result on the page

xml_stream_parser($s, function($xml) { // receives a complete xml document
	echo ($xml);
	
}, 1);

 

Link to comment
Share on other sites

That is not what I asked for. Read my question and try again.

I have the script in my htdoc folder in the xampp folder, the i will load the script like this

 

localhost/integration/rework.php
to load and connect with the device, then the output will be shown on the web browser twice and it stops
Link to comment
Share on other sites

I have the script in my htdoc folder in the xampp folder, the i will load the script like this

 

localhost/integration/rework.php
to load and connect with the device, then the output will be shown on the web browser twice and it stops

 

SO basically i'm load through a web browser

Link to comment
Share on other sites

Hi requinix

 

tried using this ajax

<script type="text/javascript">
function refresh_handler() {
    function refresh() {
       $.get('ttt,php.php', null, function(data, textStatus) {
           $("body").html(data);
        });  
	   //do something here
    }
    setInterval(refresh, 300*100); //every 20 seconds
}
$(document).ready(refresh_handler);
</script>
To call the php script

<?php
function xml_stream_parser($socket, $callback) {
    $depth = 0; // node depth
    $xml = "";  // xml string buffer

    do {
        // initialize parser
        if ($depth == 0) {
            $parser = xml_parser_create();
            // handler tracks xml node depth, builds the buffer, and invokes the callback at the end
            xml_set_default_handler($parser, function($parser, $data) use($callback, &$depth, &$xml) {
                $xml .= $data;
                if ($data[0] == "<" && $data[1] == "/") {
                    $depth--;
                    if ($depth == 0) {
                        $callback($xml);
                    }
                } else if ($data[0] == "<") {
                    $depth++;
                }
            });
        }

        // read until the parser did something
        // xml_parse natively supports a streaming approach
        do {
            $buffer = socket_read($socket, 1024, PHP_NORMAL_READ);
        } while ($buffer != "" && !xml_parse($parser, $buffer));

        if ($buffer === false) {
            trigger_error("Socket error: " . socket_strerror(socket_last_error($socket)), E_USER_WARNING);
        }

        // close the parser at the end of the document (can't reuse for another one)
        if ($depth == 0) {
            $xml = "";
            xml_parser_free($parser);
            unset($parser);
        }
    } while ($buffer != "");
}


$s = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($s, "10.184.114.43", 10001);
socket_set_nonblock($s); // block until data
socket_set_option($s, SOL_SOCKET, SO_RCVTIMEO, ["sec" => 5, "usec" => 0]); // timeout after 5 seconds
xml_stream_parser($s, function($xml) { // receives a complete xml document
		file_put_contents("intrusion.xml", $xml);
		$xmlfile = file_get_contents("intrusion.xml");
		$out= simplexml_load_string($xmlfile);
		 var_dump ($out);
		
});
socket_shutdown($s);
socket_close($s);
?>
But it is not showing any thing
Link to comment
Share on other sites

What has changed since you last said everything worked? Was that when we added the "stop after 2 documents" bit? And I see the IP address isn't 127.0.0.1 anymore?

The IP there is the real IP for the device on the network..

This is what i was trying to tell the other time, the device opens socket for every connect and if you discounect and try to connect back it will refuse until it closes connection of which they have have the issue resolve in there own monitoring software.

 

And like i said if i run the code you help write it will only echo 2 document on first connection and if AJAX calls it again it will not return any value again, until maybe i restart my system

Link to comment
Share on other sites

Need more information about the device.

 

Try using telnet on your computer - Linux, Windows, and probably Mac all work the same, though you might have to install it.

 

Open a console/terminal and execute

telnet 10.184.114.43 10001
1. What does it return? The handshake XML?

2. Does it keep running or does telnet quit?

 

If it keeps running,

3. What happens if you wait? Does it send XML when the detection event happens? Does it send anything when the detection does not happen?

 

Once you have those questions answered, quit - try Ctrl+C, or Ctrl+] and "quit", or something else. Then immediately run the command again.

4. What happens?

5. Does it differ from the first time you tried? If so, how?

Link to comment
Share on other sites

Need more information about the device.

 

Try using telnet on your computer - Linux, Windows, and probably Mac all work the same, though you might have to install it.

 

Open a console/terminal and execute

telnet 10.184.114.43 10001
1. What does it return? The handshake XML?

2. Does it keep running or does telnet quit?

 

If it keeps running,

3. What happens if you wait? Does it send XML when the detection event happens? Does it send anything when the detection does not happen?

 

Once you have those questions answered, quit - try Ctrl+C, or Ctrl+] and "quit", or something else. Then immediately run the command again.

4. What happens?

5. Does it differ from the first time you tried? If so, how?

 

When i ran the telnet it continues displaying the handshake message, so when someone touched the fiber cable outside on the fence for intrusion it sends intrusion message for few minutes and return handshake message continuously again,

When i stopped it with Ctrl+C, and execute the telnet again it timed out for a while and picked up again with the continuously handshake message when intrusion message was sent i received it and handshake continues after a while.

Link to comment
Share on other sites

The "continuous handshake" thing confuses me. Are you sure it's sending the handshake message over and over? Is it sending the handshake regardless of intrusions? Perhaps predictably every X seconds or minutes? Would it be fair to call it more of a synchronization or keep-alive message than a "handshake"?

 

As for the reconnect, I have my suspicions. One more test: run telnet in one terminal, then while that's still running do it again in a second terminal. Does the second one work?

Link to comment
Share on other sites

The "continuous handshake" thing confuses me. Are you sure it's sending the handshake message over and over? Is it sending the handshake regardless of intrusions? Perhaps predictably every X seconds or minutes? Would it be fair to call it more of a synchronization or keep-alive message than a "handshake"?

 

As for the reconnect, I have my suspicions. One more test: run telnet in one terminal, then while that's still running do it again in a second terminal. Does the second one work?

The system is called INTRUSION DETECTION SYSTEM(IDS) @Requinix, i can send you the system manual and software integration if you wish to see it. The way the system works is that it help secure the perimeter. Fiber optics cable are laid around the perimeter, and linked to the device in the server room called APU Alarm Processing Unit (FD525R)

So it receives information in real time for constant monitoring of happening in the perimeter. So the way it communicates with a client, the APU sends a platform status report (PSR) every 20 seconds and a ping response every 130 seconds in order for the system to identify the APU and to determine whether to communicate with it. When a ping request is received,

the APU sends the handshake messages. The ping in should be received by the APU within every 130 seconds, or the handshake process is diverted back to the beginning, where the system waits for the initial PSR and responding ping response messages. Once the ping in is received, the APU sends the PSR,

intrusion/alarm, fault, tamper, device configuration, and ping response messages. This successful round of communication is the “handshake,” and normal processing operations can follow uninterrupted.

 

I want to integrate this system with Ip cameras so that when i have intrusion messages it will automatically display the live video of the camera so operator can see what is going on in the perimeter, thats why i need the XML information from the device and what intrusion information that comes out is what camera video feed open.

 

I hope you get a bit of how it operates now.

I re-run the telnet, on the first window i have it running continuously and when i send intrusion message i received it, but on the second terminal it says "CANNOT CONNECT TO THE HOST ON PORT 10001". So i think its because i have the first terminal running .

 

THANK YOU

Link to comment
Share on other sites

So it sounds like the device just uses a simple socket. That means one connection at a time, and if the client disconnects then another one has to wait for the APU to discover the disconnection and close its end before a new connection will work. Which is annoying.

 

We should take a different approach now. Rather than use an AJAX request to start a connection and look for XML, run a script in the background on the "server" continuously that acts on each XML document as received. Personally I would record handshake information in a singular location and log each intrusion event as it occurs.

 

Getting that information to another PHP script for AJAX is easy. Getting that information to another PHP script instantly as events happen is not easy.

 

PHP has a few ways of exchanging information across processes, but most of the useful ones for this are geared towards Unix/Linux. Actually it's not so much about the exchanging information but about doing it in a way where the second process (the receiving end) blocks until it receives - polling is easy but you won't get the information as quickly and this feels like the sort of situation where you want to pop up the video as soon as possible.

 

What operating system is the web server running? How do you feel about installing another piece of software?

 

Even if you're on Linux, writing a good message passing system is a chore. It would be safer to install something like Redis: it offers a cache (useful for knowing the most recent event) and queues (useful for broadcasting new events as they happen). With it in place, the main script does it was originally doing - the unlimited version from the first page of this thread - but now it logs messages somewhere (preferably a database but files would work too), updates the most recent event in Redis, and broadcasts the event to a Redis queue.

 

Each AJAX script (you have to assume there could be more than one running) runs in one of two ways: running from scratch or running from a previous event. The first time it will run from scratch, meaning it only waits for the next event to happen. When it gets an event, it returns it to the calling page, which can then do whatever. That calling page then starts up another AJAX request but this time it includes the event timestamp or identifier or something; the AJAX script then runs in the second mode where it first checks if there have been any events since the one it's been told about (returning the next one, if any) or else goes into waiting mode.

 

So,

 

1. Main script starts in the background at some point

2. Main script connects to the APU and waits for XML

3. When the XML comes in, it updates the Redis cache and sends it into the Redis queue

4. It keeps running indefinitely. It could crash, so an automated process to restart the script (eg, cron) would be good.

 

On the client side,

1. The calling webpage starts the first AJAX request

2. AJAX script waits for XML to come in through the Redis queue

3. AJAX script returns it to the client, client does whatever

4. When the client is ready it starts the next AJAX request, including some information from the previous event

5. AJAX script looks for events since that last event, and if there were any returns the next one - go to 3

6. If there were no events, script waits - go to 2

 

Make sense? It might sound complicated but the real-time requirement I'm assuming you have makes this more complicated than it could otherwise be.

Link to comment
Share on other sites

So it sounds like the device just uses a simple socket. That means one connection at a time, and if the client disconnects then another one has to wait for the APU to discover the disconnection and close its end before a new connection will work. Which is annoying.

 

We should take a different approach now. Rather than use an AJAX request to start a connection and look for XML, run a script in the background on the "server" continuously that acts on each XML document as received. Personally I would record handshake information in a singular location and log each intrusion event as it occurs.

 

Getting that information to another PHP script for AJAX is easy. Getting that information to another PHP script instantly as events happen is not easy.

 

PHP has a few ways of exchanging information across processes, but most of the useful ones for this are geared towards Unix/Linux. Actually it's not so much about the exchanging information but about doing it in a way where the second process (the receiving end) blocks until it receives - polling is easy but you won't get the information as quickly and this feels like the sort of situation where you want to pop up the video as soon as possible.

 

What operating system is the web server running? How do you feel about installing another piece of software?

 

Even if you're on Linux, writing a good message passing system is a chore. It would be safer to install something like Redis: it offers a cache (useful for knowing the most recent event) and queues (useful for broadcasting new events as they happen). With it in place, the main script does it was originally doing - the unlimited version from the first page of this thread - but now it logs messages somewhere (preferably a database but files would work too), updates the most recent event in Redis, and broadcasts the event to a Redis queue.

 

Each AJAX script (you have to assume there could be more than one running) runs in one of two ways: running from scratch or running from a previous event. The first time it will run from scratch, meaning it only waits for the next event to happen. When it gets an event, it returns it to the calling page, which can then do whatever. That calling page then starts up another AJAX request but this time it includes the event timestamp or identifier or something; the AJAX script then runs in the second mode where it first checks if there have been any events since the one it's been told about (returning the next one, if any) or else goes into waiting mode.

 

So,

 

1. Main script starts in the background at some point

2. Main script connects to the APU and waits for XML

3. When the XML comes in, it updates the Redis cache and sends it into the Redis queue

4. It keeps running indefinitely. It could crash, so an automated process to restart the script (eg, cron) would be good.

 

On the client side,

1. The calling webpage starts the first AJAX request

2. AJAX script waits for XML to come in through the Redis queue

3. AJAX script returns it to the client, client does whatever

4. When the client is ready it starts the next AJAX request, including some information from the previous event

5. AJAX script looks for events since that last event, and if there were any returns the next one - go to 3

6. If there were no events, script waits - go to 2

 

Make sense? It might sound complicated but the real-time requirement I'm assuming you have makes this more complicated than it could otherwise be.

Thank very much requinix, But here is the situation. I don't have control over the server (APU device)It is a hardware running communication on socket, an Original Equipment Manufacturer (Fiber Sensys Inc)developed it for their IDS system, So the device is running on .NET, information is passed electronically through fiber optics to the device in form of ASCII code based on my understanding of it operations

So i guess they compiled it in such a way that it generate XML based on the ASCII string which is being forwarded to the network for a client to communicate and exchange information.

Basically the Manufacturer develop the device, i just want to work with it to achieve my own aim, they actually have A SDK which you can work with in order to integrate it with any thing you want, but i couldn't use it because its written in .NET framework(C#), thats why i have deceided to use PHP by getting information through the socket

 

I hope this explains it better.

Thank you very much

Regards

Link to comment
Share on other sites

I don't understand the question, I'm running the PHP script on my PC to connect to the device. Which is running on .NET framework. There is no other web server

Are you talking about the server running on my own PC, i have XAMPP installed and i'm using windows 7.
Link to comment
Share on other sites

Correct, and he is suggesting you run Redis on your workstation, so it's available to your PHP script for message passing and pub/sub. Some memory will need to be allocated to it, exactly as you are already allocating memory to run the web server and php script(s) you are developing for this.

 

While Redis doesn't officially support running under Windows, microsoft has done the port: https://github.com/MicrosoftArchive/redis

Link to comment
Share on other sites

Correct, and he is suggesting you run Redis on your workstation, so it's available to your PHP script for message passing and pub/sub. Some memory will need to be allocated to it, exactly as you are already allocating memory to run the web server and php script(s) you are developing for this.

 

While Redis doesn't officially support running under Windows, microsoft has done the port: https://github.com/MicrosoftArchive/redis

Thank you Gizmola.. I got it now, but please can any of you guys just put me through this process, may be i could resolve it or should i just use the device's SDK for a .NET developer to use and achieve this thing, what would you suggest ?

Thanks to you and requinix

Link to comment
Share on other sites

Have you installed it? They have an installer and everything.

Yes have downloaded, and installed, please what is the next step to take... Thank you very much sir.. Was waiting for you days now, tho tried somethings with PHP-REDIS but nothing is coming out
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • 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.