Jump to content

Error parsing xml data into array


rushman005

Recommended Posts

Hi guys how are you 

I have been battling with this xml interpretation for months now.

I have a TCP client on aa network i'm connecting to its port using php socket. I used php socket function which reads from the port of the device. On connecting to the device it will interchange a handshake message with it. and if there is another event another message will be forwarded.

 

below is the code i'm using 

<?php
//timeout limit
set_time_limit(0);
 
//Creating Socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
   // echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "<br />";
} else {
   // echo "OK.<br />";
}
//10.33.255.238____10.184.114.43
//echo "Attempting to connect to the socket'...";
$result = socket_connect($socket, '127.0.0.1', 10001);
 
if ($result === false) {
   // echo "socket_connect() failed.<br />Reason: ($result) " . socket_strerror(socket_last_error($socket)) . "<br />";
} else {
  //  echo "OK.<br />";
}
$in = "GET / HTTP/1.1\r<br />";
$in .= "Host: 10.184.114.43\r<br />";
$in .= "Content-Type: application/xml; version=1.0 encoding=UTF-8\r\n";
$in .= "Accept: application/xml\n";
$in .= "Connection: Close\r<br />\r<br />";
$out= '';
while (  $xml = socket_read($socket, 100000)){
                                $out= simplexml_load_string($xml);
                echo htmlspecialchars ($out);
                flush();
                ob_flush();
                ob_implicit_flush();
                sleep (85);
                }
               
socket_close($socket);
?>

Here is the error i'm getting with the above code

Warning: simplexml_load_string(): Entity: line 2: parser error : XML declaration allowed only at the start of the document in C:\xampp\htdocs\integration\ttt,php.php on line 30



Warning: simplexml_load_string(): <?xml version="1.0" enco in C:\xampp\htdocs\integration\ttt,php.php on line 30



Warning: simplexml_load_string(): ^ in C:\xampp\htdocs\integration\ttt,php.php on line 30



Warning: simplexml_load_string(): Entity: line 2: parser error : ParsePI: PI xml never end ... in C:\xampp\htdocs\integration\ttt,php.php on line 30



Warning: simplexml_load_string(): <?xml version="1.0" enco in C:\xampp\htdocs\integration\ttt,php.php on line 30



Warning: simplexml_load_string(): ^ in C:\xampp\htdocs\integration\ttt,php.php on line 30



Warning: simplexml_load_string(): Entity: line 2: parser error : Start tag expected, '<' not found in C:\xampp\htdocs\integration\ttt,php.php on line 30



Warning: simplexml_load_string(): <?xml version="1.0" enco in C:\xampp\htdocs\integration\ttt,php.php on line 30



Warning: simplexml_load_string(): ^ in C:\xampp\htdocs\integration\ttt,php.php on line 30



Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in C:\xampp\htdocs\integration\ttt,php.php on line 30



Warning: simplexml_load_string(): ding="UTF-8"?> in C:\xampp\htdocs\integration\ttt,php.php on line 30



Warning: simplexml_load_string(): ^ in C:\xampp\htdocs\integration\ttt,php.php on line 30

below is the handshake xml data that will be forwarded on first connection to the port

 
<?xml version="1.0" encoding="UTF-8"?>
<PlatformStatusReport>
<PlatformIdentification>
 <DeviceName>FD525R-103852</DeviceName>
<DeviceCategory>Sensor</DeviceCategory>
<DeviceType>SPIDR APU</DeviceType>
</PlatformIdentification>
 <PlatformStatusReport>
<PlatformIdentification>
<DeviceName>FD525R-103852.HZONE-1</DeviceName>
<DeviceCategory>Sensor</DeviceCategory>
<DeviceType>SPIDR Hyperzone</DeviceType>
</PlatformIdentification>
 <Status>
<DeviceState>Secure</DeviceState>
<CommunicationState>OK</CommunicationState>
<UpdateTime Zone="GMT">2017-10-25T14:56:36.862</UpdateTime>
 </Status>
 <DeviceStatusReport>
<DeviceIdentification>
<DeviceName>FD525R-103852.HZONE-1.ZONE-001</DeviceName> <DeviceCategory>Sensor</DeviceCategory>
                                                <DeviceType>SPIDR Zone</DeviceType>
</DeviceIdentification>
<Status>
<DeviceState>Secure</DeviceState>
<CommunicationState>OK</CommunicationState>
<UpdateTime Zone="GMT">2017-10-25T14:56:36.863</UpdateTime>
</Status>
 </DeviceStatusReport>
 <DeviceStatusReport>
<DeviceIdentification>
<DeviceName>FD525R-103852.HZONE-1.ZONE-002</DeviceName> <DeviceCategory>Sensor</DeviceCategory>
<DeviceType>SPIDR Zone</DeviceType>
</DeviceIdentification>
<Status>
<DeviceState>Secure</DeviceState>
<CommunicationState>OK</CommunicationState>
 <UpdateTime Zone="GMT">2017-10-25T14:56:36.864</UpdateTime>
                </Status>
 </DeviceStatusReport>
<DeviceStatusReport>
 <DeviceIdentification>
<DeviceName>FD525R-103852.HZONE-1.ZONE-003</DeviceName> <DeviceCategory>Sensor</DeviceCategory>
<DeviceType>SPIDR Zone</DeviceType>
</DeviceIdentification>
 <Status>
<DeviceState>Secure</DeviceState>
 <CommunicationState>OK</CommunicationState>
<UpdateTime Zone="GMT">2017-10-25T14:56:36.864</UpdateTime>
</Status>
 </DeviceStatusReport>
 <DeviceStatusReport>
 <DeviceIdentification>
<DeviceName>FD525R-103852.HZONE-1.ZONE-006</DeviceName> <DeviceCategory>Sensor</DeviceCategory>
<DeviceType>SPIDR Zone</DeviceType>
</DeviceIdentification>
<Status>
<DeviceState>Secure</DeviceState>
<CommunicationState>OK</CommunicationState>
<UpdateTime Zone="GMT">2017-10-25T14:56:36.865</UpdateTime>
</Status>
 </DeviceStatusReport>
 <DeviceStatusReport>
 <DeviceIdentification>
 <DeviceName>FD525R-103852.HZONE-1.ZONE-007</DeviceName> <DeviceCategory>Sensor</DeviceCategory>
 <DeviceType>SPIDR Zone</DeviceType>
 </DeviceIdentification>
<Status>
<DeviceState>Secure</DeviceState>
<CommunicationState>OK</CommunicationState>
<UpdateTime Zone="GMT">2017-10-25T14:56:36.866</UpdateTime>
</Status>
 </DeviceStatusReport>
 <DeviceStatusReport>
                                <DeviceIdentification>
<DeviceName>FD525R-103852.HZONE-1.ZONE-008</DeviceName> <DeviceCategory>Sensor</DeviceCategory>
 <DeviceType>SPIDR Zone</DeviceType>
 </DeviceIdentification>
 <Status>
<DeviceState>Secure</DeviceState>
 <CommunicationState>OK</CommunicationState>
 <UpdateTime Zone="GMT">2017-10-25T14:56:36.867</UpdateTime>
</Status>
                </DeviceStatusReport>
 <DeviceStatusReport>
<DeviceIdentification>
<DeviceName>FD525R-103852.HZONE-1.ZONE-009</DeviceName> <DeviceCategory>Sensor</DeviceCategory>
<DeviceType>SPIDR Zone</DeviceType>
</DeviceIdentification>
<Status>
                                <DeviceState>Secure</DeviceState>
                                <CommunicationState>OK</CommunicationState>
<UpdateTime Zone="GMT">2017-10-25T14:56:36.868</UpdateTime>
</Status>
</DeviceStatusReport>
<DeviceStatusReport>
<DeviceIdentification>
<DeviceName>FD525R-103852.HZONE-1.ZONE-010</DeviceName> <DeviceCategory>Sensor</DeviceCategory>
<DeviceType>SPIDR Zone</DeviceType>
 </DeviceIdentification>
                <Status>
                                <DeviceState>Secure</DeviceState>
<CommunicationState>OK</CommunicationState>
<UpdateTime Zone="GMT">2017-10-25T14:56:36.869</UpdateTime>
 </Status>
</DeviceStatusReport>
<DeviceStatusReport>
<DeviceIdentification>
<DeviceName>FD525R-103852.HZONE-1.ZONE-011</DeviceName> <DeviceCategory>Sensor</DeviceCategory>
<DeviceType>SPIDR Zone</DeviceType>
</DeviceIdentification>
 <Status>
<DeviceState>Secure</DeviceState>
 <CommunicationState>OK</CommunicationState>
<UpdateTime Zone="GMT">2017-10-25T14:56:36.870</UpdateTime>
 </Status>
</DeviceStatusReport>
<DeviceStatusReport>
<DeviceIdentification>
<DeviceName>FD525R-103852.HZONE-1.ZONE-012</DeviceName> <DeviceCategory>Sensor</DeviceCategory>
 <DeviceType>SPIDR Zone</DeviceType>
</DeviceIdentification>
 <Status>
<DeviceState>Secure</DeviceState>
 <CommunicationState>OK</CommunicationState>
 <UpdateTime Zone="GMT">2017-10-25T14:56:36.871</UpdateTime>
 </Status>
 </DeviceStatusReport>
 <DeviceStatusReport>
<DeviceIdentification>
<DeviceName>FD525R-103852.HZONE-1.ZONE-013</DeviceName> <DeviceCategory>Sensor</DeviceCategory>
 <DeviceType>SPIDR Zone</DeviceType>
 </DeviceIdentification>
<Status>
 <DeviceState>Secure</DeviceState>
<CommunicationState>OK</CommunicationState>
 <UpdateTime Zone="GMT">2017-10-25T14:56:36.871</UpdateTime>
</Status>
 </DeviceStatusReport>
</PlatformStatusReport>
<PlatformStatusReport>
 <PlatformIdentification>
 <DeviceName>FD525R-103852.HZONE-2</DeviceName>
<DeviceCategory>Sensor</DeviceCategory>
 <DeviceType>SPIDR Hyperzone</DeviceType>
                </PlatformIdentification>
<Status>
<DeviceState>Secure</DeviceState>
 <CommunicationState>OK</CommunicationState>
<UpdateTime Zone="GMT">2017-10-25T14:56:36.872</UpdateTime>
 </Status>
 <DeviceStatusReport>
<DeviceIdentification>
 <DeviceName>FD525R-103852.HZONE-2.ZONE-004</DeviceName> <DeviceCategory>Sensor</DeviceCategory>
 <DeviceType>SPIDR Zone</DeviceType>
 </DeviceIdentification>
<Status>
<DeviceState>Secure</DeviceState>
 <CommunicationState>OK</CommunicationState>
<UpdateTime Zone="GMT">2017-10-25T14:56:36.873</UpdateTime>
 </Status>
</DeviceStatusReport>
<DeviceStatusReport>
<DeviceIdentification>
<DeviceName>FD525R-103852.HZONE-2.ZONE-005</DeviceName> <DeviceCategory>Sensor</DeviceCategory>
                <DeviceType>SPIDR Zone</DeviceType>
</DeviceIdentification>
<Status>
<DeviceState>Secure</DeviceState>
<CommunicationState>OK</CommunicationState>
<UpdateTime Zone="GMT">2017-10-25T14:56:36.874</UpdateTime>
 </Status>
</DeviceStatusReport>
 </PlatformStatusReport>
 </PlatformStatusReport>

This is the second message which i'm trying to pick out some information like DeviceName and DetectionEvent from it 

<?xml version="1.0" encoding="UTF-8"?>
<DeviceDetectionReport>

<DeviceDetectionRecord>

<DeviceIdentification>

<DeviceName>FD525R.HZONE-1.ZONE-1</DeviceName>

<DeviceCategory>Sensor</DeviceCategory>

<DeviceType>SPIDR Zone</DeviceType>

</DeviceIdentification>

<Detection>

<ID>SZ003</ID>

<DetectionEvent>Intrusion</DetectionEvent>

<UpdateTime Zone="GMT">2017-10-24T16:15:10.096</UpdateTime>

</Detection>

</DeviceDetectionRecord>

</DeviceDetectionReport>

Please how do i pass this xml data and pick out some information which will be saved in a variable for further usage.

 

Thank you

 

Edit By Psycho: Fixed code tags

Edited by Psycho
Link to comment
Share on other sites

while (  $xml = socket_read($socket, 100000)){
You can't assume the entire XML document was read in that one call.

 

Build up a string buffer of the XML contents and only try to parse it after you've read everything and the connection has closed.

 

Meanwhile the first error message may mean it didn't like the whitespace at the beginning. Which would be unfortunate. If that's the case, ltrim() it out.

Link to comment
Share on other sites

HI requinix, 

I'm actually getting frustrated of the project because i have limited knowledge. Can you please help me come up with the code to eliminate this whitespaces. I'm very sure im getting the whole content because when i echo the variable $xml it will give out the full xml out put with the flush(); function.

 

Thank you

Link to comment
Share on other sites

Can you please help me come up with the code to eliminate this whitespaces.

I mentioned ltrim.

 

I'm very sure im getting the whole content because when i echo the variable $xml it will give out the full xml out put with the flush(); function.

But you're echoing inside the loop, which means you'll echo each $xml as it comes in. There's no way for you to tell whether you're getting all the XML at once, and I suspect you are not.

 

Fix the code so that you are getting a partial string with the socket_read() and appending it to another variable. When the loop ends because there's nothing left to read, then try to parse it.

Link to comment
Share on other sites

I mentioned ltrim.

 

But you're echoing inside the loop, which means you'll echo each $xml as it comes in. There's no way for you to tell whether you're getting all the XML at once, and I suspect you are not.

 

Fix the code so that you are getting a partial string with the socket_read() and appending it to another variable. When the loop ends because there's nothing left to read, then try to parse it.

Please Requinix, How do i go about that, I don't mind if i have to do anything, can we troubleshoot it together.. You can tell me a time when you will be free so we do it together

 

Thank you 

regards

Link to comment
Share on other sites

Please Requinix, How do i go about that, I don't mind if i have to do anything, can we troubleshoot it together.. You can tell me a time when you will be free so we do it together

 

Thank you 

regards

This is what i'm trying to do now

 

while (  $xml = socket_read($socket, 100000)){
                               
                flush();
                ob_flush();
                ob_implicit_flush();
                sleep (85);
                }
$xml = ltrim($xml);
$out= simplexml_load_string($xml);
                echo htmlspecialchars ($out);
               
socket_close($socket);
 
but i'm not getting any response
Link to comment
Share on other sites

You have the ltrim part correct but the string thing whatever I said isn't quite. You're just reassigning $xml every time. The previous value will get lost.

 

You also don't need the flushing stuff, and the sleeping means you're guaranteed to wait at least 85 seconds until the XML stuff really gets going. I don't think you really know what you're doing.

 

And now that I really look at this, all you're doing is a regular HTTP request against the service. It's also the sort of thing that doesn't exactly support the "forwarding" you talked about in your first post.

 

Let's go back to basics. What does this do?

<?php

$xml = simplexml_load_file("http://127.0.0.1:10001/");
var_dump($xml);
Link to comment
Share on other sites

You have the ltrim part correct but the string thing whatever I said isn't quite. You're just reassigning $xml every time. The previous value will get lost.

 

You also don't need the flushing stuff, and the sleeping means you're guaranteed to wait at least 85 seconds until the XML stuff really gets going. I don't think you really know what you're doing.

 

And now that I really look at this, all you're doing is a regular HTTP request against the service. It's also the sort of thing that doesn't exactly support the "forwarding" you talked about in your first post.

 

Let's go back to basics. What does this do?

<?php

$xml = simplexml_load_file("http://127.0.0.1:10001/");
var_dump($xml);

Its loads file from that ip and port and display it pegged to an array.

Link to comment
Share on other sites

OKay boss, lemme explain what i want to achieve may you have a better way. I have the TCP client (Its an hardware called Alarm Processing Unit), it is place on a network and fiber cables are connected to it. It is use for perimeter security. IF someone tries to intrude and they step on the fiber cable in the perimeter it sends a signal through fiber cable to the device, and the ASCII code is converted to XML generated and being forwarded to the port of the device 127.0.0.1:10001.

 

So this information is what i want to collect and process to call up a camera attached to that zone for the operator to automatically see what is going on.

Immediately a client connect to the port you receive a handshake message and if there is an intrusion you get an intrusion message. The device send these information to the network every 130 secs after another update comes. i Just want to have a program that waits for the intrusion message and use information like DeviceName, Detection to bring up a camera to display 

 

I hope you understand it 

Thanks 

Regards

Link to comment
Share on other sites

Okay, so you need a persistent PHP script that just sits and waits for stuff to come over the socket?

 

PHP doesn't have a stream-based XML reader that can handle multiple XML documents, so here's this I just wrote to do it.

function xml_stream_parser($handle, $callback) {
	$depth = 0; // node depth
	$xml = "";  // xml string buffer

	while (!feof($handle)) {
		// 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
		while (!xml_parse($parser, fgets($handle)) && !feof($handle));

		// 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);
		}
	}
}
Use like

$h = fopen("tcp://127.0.0.1:10001", "rt");
stream_set_blocking($h, true); // block until data
stream_set_timeout($h, 5, 0);  // timeout after 5 seconds
xml_stream_parser($h, function($xml) { // receives a complete xml document
	// deal with $xml
});
fclose($h);
Note the $handle is a stream resource, not a socket. PHP's streams are better than its sockets in nearly every way.

The $xml doesn't include the processing instruction (

 

I didn't test with a true socket but it did work with a test stream I set up that slowly served pieces of XML.

<?php

$h = fopen(__FILE__, "rt");
fseek($h, __COMPILER_HALT_OFFSET__);
while (!feof($h)) {
	echo fgets($h);
	sleep(1);
}

__halt_compiler();

<?xml version="1.0" encoding="utf-8"?>
<a aa="aa">
<aaa/>
</a>



<?xml version="1.0" encoding="utf-8"?>
<b bb="bb">
<bbb/></b>

<?xml version="1.0" encoding="utf-8"?>
<c cc="cc"><ccc/>
</c>
  • Like 1
Link to comment
Share on other sites

Okay, so you need a persistent PHP script that just sits and waits for stuff to come over the socket?

 

PHP doesn't have a stream-based XML reader that can handle multiple XML documents, so here's this I just wrote to do it.

function xml_stream_parser($handle, $callback) {
	$depth = 0; // node depth
	$xml = "";  // xml string buffer

	while (!feof($handle)) {
		// 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
		while (!xml_parse($parser, fgets($handle)) && !feof($handle));

		// 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);
		}
	}
}
Use like

$h = fopen("tcp://127.0.0.1:10001", "rt");
stream_set_blocking($h, true); // block until data
stream_set_timeout($h, 5, 0);  // timeout after 5 seconds
xml_stream_parser($h, function($xml) { // receives a complete xml document
	// deal with $xml
});
fclose($h);
Note the $handle is a stream resource, not a socket. PHP's streams are better than its sockets in nearly every way.

The $xml doesn't include the processing instruction (<?xml...?>) because a quick attempt to add that in didn't work. You should be fine without it, or you can just assume one before handing $xml to SimpleXML.

 

I didn't test with a true socket but it did work with a test stream I set up that slowly served pieces of XML.

<?php

$h = fopen(__FILE__, "rt");
fseek($h, __COMPILER_HALT_OFFSET__);
while (!feof($h)) {
	echo fgets($h);
	sleep(1);
}

__halt_compiler();

<?xml version="1.0" encoding="utf-8"?>
<a aa="aa">
<aaa/>
</a>



<?xml version="1.0" encoding="utf-8"?>
<b bb="bb">
<bbb/></b>

<?xml version="1.0" encoding="utf-8"?>
<c cc="cc"><ccc/>
</c>

Forgot to tell you that it does not allow tcp layer streaming. It does not allow that;

 

Here is the error i got when i tried what you have wrote..

 

Warning: fopen(): Unable to find the wrapper "tcp" - did you forget to enable it when you configured PHP? in C:\xampp\htdocs\integration\fullpackage.php on line 37

 

Warning: fopen(tcp://192.168.0.78:10001): failed to open stream: Invalid argument in C:\xampp\htdocs\integration\fullpackage.php on line 37

 

Warning: stream_set_blocking() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\integration\fullpackage.php on line 38

 

Warning: stream_set_timeout() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\integration\fullpackage.php on line 39

 

Warning: feof() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\integration\fullpackage.php on line 6

 

Warning: fgets() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\integration\fullpackage.php on line 26

 

Warning: feof() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\integration\fullpackage.php on line 6

 

Warning: fgets() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\integration\fullpackage.php on line 26

 

We can do a team viewer package if you don't mind, so we can work directly on the system, then may be i can show you some things live.. I really appreciate all you have been doing, you are a genius ....

Link to comment
Share on other sites

I forget what it takes to get tcp:// and udp:// support...

 

All those errors are stemming from the stream not being opened, so here's a sockets version.

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);
			if ($buffer === false && socket_last_error($socket) != ???) {
				// read timeout
				continue;
			}
		} 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 != "");
}
There's a ??? you need to fill in for the error code of a read timeout. It varies by system and I'm not sure if there's a portable way to get it. On Linux it's possibly EAGAIN or EWOULDBLOCK (check your recv(3) docs), however PHP doesn't expose those as constants IIRC, and on Windows it's a five digit number like 10007 or something I don't quite remember.

 

And then

$s = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($s, "127.0.0.1", 10001);
socket_set_block($s); // block until data (or timeout)
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
	// deal with $xml
});
socket_shutdown($s);
socket_close($s);
I think. Edited by requinix
the ??? bit
Link to comment
Share on other sites

I forget what it takes to get tcp:// and udp:// support...

 

All those errors are stemming from the stream not being opened, so here's a sockets version.

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);
			if ($buffer === false && socket_last_error($socket) != ???) {
				// read timeout
				continue;
			}
		} 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 != "");
}
There's a ??? you need to fill in for the error code of a read timeout. It varies by system and I'm not sure if there's a portable way to get it. On Linux it's possibly EAGAIN or EWOULDBLOCK (check your recv(3) docs), however PHP doesn't expose those as constants IIRC, and on Windows it's a five digit number like 10007 or something I don't quite remember.

 

And then

$s = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($s, "127.0.0.1", 10001);
socket_set_block($s); // block until data (or timeout)
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
	// deal with $xml
});
socket_shutdown($s);
socket_close($s);
I think.

 

Wow! You are such a nice guy... Always ready to help thanks 

 

See the error i received below

Warning: Socket error: A non-blocking socket operation could not be completed immediately. in C:\xampp\htdocs\integration\fullpackage.php on line 31

 

PLEASE IF YOU ARE ON SKYPE OR TEAM VIEWER LETS WORK TOGETHER ON THAT... PLEASE YOU GET MORE DETAILS YOURSELF

Link to comment
Share on other sites

Where is the error happening? What does socket_last_error() return and what operating system is it? For socket work PHP mostly just wraps lower-level system calls like recv(3) so look to those for explanations of error codes.

The socket should be blocking... Even with it, does the code work? Worst case and last resort you could @-suppress the function call.

 

And I'm not on Skype or TeamViewer. Even if I was, it's time for me to go to bed. The hard part of all this was the fact that one "resource" could provide multiple XML documents and I'm confident my code gets you at least 90% of the way there, so whatever may be remaining shouldn't be too hard to figure out.

 

Oh. I will say that there is one potential problem with my stuff: if two documents are received at the same time, the socket_read() could include the tail end of one and the beginning of the other. That won't work. If that's a risk (and I think it is) then change the one socket_read() call to be

socket_read($socket, 1024, PHP_NORMAL_READ)
so it will stop at a line ending. The code could still break if there was not a line ending between the two documents but I don't think that is the case. Ideally all this would be a true stateful character-by-character parser but there isn't one [that can support multiple documents] built into PHP and that means more work, possibly reduced by using a third-party library, so hopefully that's not required and the line-by-line approach there now will work.
  • Like 1
Link to comment
Share on other sites

Where is the error happening? What does socket_last_error() return and what operating system is it? For socket work PHP mostly just wraps lower-level system calls like recv(3) so look to those for explanations of error codes.

The socket should be blocking... Even with it, does the code work? Worst case and last resort you could @-suppress the function call.

 

And I'm not on Skype or TeamViewer. Even if I was, it's time for me to go to bed. The hard part of all this was the fact that one "resource" could provide multiple XML documents and I'm confident my code gets you at least 90% of the way there, so whatever may be remaining shouldn't be too hard to figure out.

 

Oh. I will say that there is one potential problem with my stuff: if two documents are received at the same time, the socket_read() could include the tail end of one and the beginning of the other. That won't work. If that's a risk (and I think it is) then change the one socket_read() call to be

socket_read($socket, 1024, PHP_NORMAL_READ)
so it will stop at a line ending. The code could still break if there was not a line ending between the two documents but I don't think that is the case. Ideally all this would be a true stateful character-by-character parser but there isn't one [that can support multiple documents] built into PHP and that means more work, possibly reduced by using a third-party library, so hopefully that's not required and the line-by-line approach there now will work.

 

Thank you so much requinix...

 

The Code is working perfectly and echoing the full XML output data without any one being truncated. Did that with the code below 

 

xml_stream_parser($s, function($xml) { // receives a complete xml document
 
//echo htmlspecialchars($xml);// deal with $xml
 
});
 
But when i tried parsing the string into an array to pick some information with this code 
 
xml_stream_parser($s, function($xml) { // receives a complete xml document
//echo htmlspecialchars($xml);// deal with $xml
$out = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($out);
$array = json_decode($json,TRUE);
$device_name = $array["PlatformStatusReport"]["PlatformIdentification"]["DeviceName"];
 
echo $device_name;
 
});
 
It kept on loading without any result...
 
i also tried using this 
 
xml_stream_parser($s, file_put_content("intrusion.xml", function($xml) { // receives a complete xml document
 
$out = simplexml_load_string(file_get_contents("intrusion.xml", "SimpleXMLElement", LIBXML_NOCDATA));
 
if (isset($out->PlatformStatusReport)){
$device_name = $out->PlatformStatusReport->PlatformIdentification->DeviceName. PHP_EOL;
echo $device_name;
}
 
 
}));
 
It showed an error that this line of code $callback($xml); is not a string
 
I just want to pick some information from the XML data based on the current data its holding. Either PlatformStatusReport or DeviceDetectionRecord
 
You have practically proven to me beyond all benefit of doubt that you are a total genius
please if you trade bitcoin kindly give me your wallet address or any paypal account... You deserve the best.
 
You really need rest, may be tomorrow morning we could use teamviewer if you can, just to download it install and you can have access to my system remotely..
 
Thank you, please if you trade bitcoin send me your wallet address or if its paypal
 
Thanks bro
Link to comment
Share on other sites

This whole script is reading XML from a socket, as each document arrives. You can't treat it like a normal PHP script with regular output because it will keep running indefinitely.

 

If you do want regular output, as in it receives the handshake, receives a fixed number of XML documents (like 1), and then stops, then you have to do that explicitly: track the number of documents received, either in xml_stream_parser (my recommendation) or in the callback, and then stop the script when you want it to stop.

 

What is the purpose of this script? What are you ultimately trying to do with it?

Link to comment
Share on other sites

This whole script is reading XML from a socket, as each document arrives. You can't treat it like a normal PHP script with regular output because it will keep running indefinitely.

 

If you do want regular output, as in it receives the handshake, receives a fixed number of XML documents (like 1), and then stops, then you have to do that explicitly: track the number of documents received, either in xml_stream_parser (my recommendation) or in the callback, and then stop the script when you want it to stop.

 

What is the purpose of this script? What are you ultimately trying to do with it?

Thank you very much Requinix

 

What i want to achieve is to check if the xml output is DeviceDetectionRecord parent tag, 

Then i want to collect some informations like "DeviceName" "DetectionEvent", "UpdateTime" from the field and use it to trigger a camera live feed just like i did in the code below;

 

$out (is the variable of the xml output i parsed with simplexml_load_string), But remember simplexml_load_string was throwing the error i showed in the beginning of the thread.

 if (isset ( $out->DeviceDetectionRecord )) {

    $identity= "<strong>". $out->DeviceDetectionRecord->Detection->ID ."</strong>". PHP_EOL;
//echo "ID:" .$identity;
$intrutionOrTamper = $out->DeviceDetectionRecord->Detection->DetectionEvent.PHP_EOL;
    $detect = "<strong>".$out->DeviceDetectionRecord->Detection->DetectionEvent."</strong>". PHP_EOL;
    $date= "<strong>".$out->DeviceDetectionRecord->Detection->UpdateTime ."</strong>". PHP_EOL;
//echo "UpdateTime:" .$date;
 
$deviceName = $out->DeviceDetectionRecord->DeviceIdentification->DeviceName;
$zone = explode('.', $deviceName)[2];
$hyper = explode('.',$deviceName)[1];
echo "Detection Event:"." ". $intrutionOrTamper." "."From"." ".$zone." "."By"." ".$date."</br><hr>";
 
//<img src="images/slider2.jpg" id="background">
if (trim($intrutionOrTamper) == "Intrusion"){
if ($zone == 'ZONE-1' && $hyper == 'HZONE-1'){
echo '<script>window.open("cameras/zone1.php", "_blank")</script>'; 
echo '<script> document.getElementById("ground").src="images/z1.gif"</script>';
}elseif($zone == 'ZONE-2' && $hyper == 'HZONE-1'){
echo '<script>window.open("cameras/zone2.php", "_blank")</script>';
echo '<script>window.open("cameras/integrationById("ground").src="images/slider1.jpg"</script>';
}elseif($zone == 'ZONE-3' && $hyper == 'HZONE-1'){
echo '<script>window.open("cameras/zone3.php", "_blank")</script>'; 
echo '<script> document.getElementById("ground").src="images/slider1.jpg"</script>';
}elseif($zone == 'ZONE4' && $hyper == 'HZONE1'){
echo '<script>window.open("cameras/zone4.php", "_blank")</script>'; 
echo '<script> document.getElementById("ground").src="images/slider1.jpg"</script>';
}
elseif($zone == 'ZONE5' && $hyper == 'HZONE1'){
echo '<script>window.open("cameras/zone5.php", "_blank")</script>'; 
echo '<script> document.getElementById("ground").src="images/slider1.jpg"</script>';
}
elseif($zone == 'ZONE6' && $hyper == 'HZONE1'){
echo '<script>window.open("cameras/zone6.php", "_blank")</script>'; 
echo '<script> document.getElementById("ground").src="images/slider1.jpg"</script>';
}
elseif($zone == 'ZONE7' && $hyper == 'HZONE1'){
echo '<script>window.open("cameras/zone7.php", "_blank")</script>'; 
echo '<script> document.getElementById("ground").src="images/slider1.jpg"</script>';
}
elseif($zone == 'ZONE8' && $hyper == 'HZONE1'){
echo '<script>window.open("cameras/zone8.php", "_blank")</script>'; 
echo '<script> document.getElementById("ground").src="images/slider1.jpg"</script>';
}
elseif($zone == 'ZONE9' && $hyper == 'HZONE1'){
echo '<script>window.open("cameras/zone9.php", "_blank")</script>'; 
echo '<script> document.getElementById("ground").src="images/slider1.jpg"</script>';
}
elseif($zone == 'ZONE10' && $hyper == 'HZONE1'){
echo '<script>window.open("cameras/zone10.php", "_blank")</script>'; 
echo '<script> document.getElementById("ground").src="images/slider1.jpg"</script>';
 
HERE IS SAMPLE OF THE XML OUT 

<?xml version="1.0" encoding="UTF-8"?>

<DeviceDetectionReport>

  <DeviceDetectionRecord>

    <DeviceIdentification>

      <DeviceName>ras.H1.Z1</DeviceName>

      <DeviceCategory>Sensor</DeviceCategory>

      <DeviceType>SPIDR Zone</DeviceType>

    </DeviceIdentification>

    <Detection>

      <ID>SZ002</ID>

      <DetectionEvent>Intrusion</DetectionEvent>

      <UpdateTime Zone="GMT">2017-10-18T09:38:04.103</UpdateTime>

    </Detection>

  </DeviceDetectionRecord>

</DeviceDetectionReport>

 

PLEASE DO REMEMBER TO SEND ME THE BITCOIN ADDRESS IF YOU TRADE IT OR PAYPAL PLEASE YOU HAVE BEEN EXCEPTIONAL AND VERY KIND TO ME 

Link to comment
Share on other sites

I just decided to test the code again this morning see the error i'm getting 

 

Warning: 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 C:\xampp\htdocs\integration\rework.php on line 27

Warning: 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 C:\xampp\htdocs\integration\rework.php on line 35

 

Here is my full code snippet

 

<?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, 2048, PHP_NORMAL_READ);
if ($buffer === false && socket_last_error($socket) != '73730') {
// read timeout
continue;
}
} 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 != "");
}
set_time_limit(0);
$s = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($s, "10.184.114.43", 10001);
socket_set_block($s); // block until data (or timeout)
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
if (isset ( $xml->DeviceDetectionRecord )) {
    $identity= "<strong>". $out->DeviceDetectionRecord->Detection->ID ."</strong>". PHP_EOL;
//echo "ID:" .$identity;
$intrutionOrTamper = $xml->DeviceDetectionRecord->Detection->DetectionEvent.PHP_EOL;
    $detect = "<strong>".$xml->DeviceDetectionRecord->Detection->DetectionEvent."</strong>". PHP_EOL;
    $date= "<strong>".$xml->DeviceDetectionRecord->Detection->UpdateTime ."</strong>". PHP_EOL;
//echo "UpdateTime:" .$date;
 
$deviceName = $xml->DeviceDetectionRecord->DeviceIdentification->DeviceName;
$zone = explode('.', $deviceName)[2];
$hyper = explode('.',$deviceName)[1];
echo "Detection Event:"." ". $intrutionOrTamper." "."From"." ".$zone." "."By"." ".$date."</br><hr>";
}else{
 
echo htmlspecialchars($xml);
}
});
socket_shutdown($s);
socket_close($s);
?>
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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, 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.

Please let me pay you to get this thing done for me... Honestly its beyond my PHP understanding and this is not a project i can learn with, may be by taking weeks or months to do it.. It is a project i'm working on for my company and the timeline has been exceeded. Receiving a load of pressure here from the project manager.. 

So i'm outrightly confused right now.. PLEASE HELP ME

Link to comment
Share on other sites

No. And I forgot to mention, those other errors sound like the server is down. You're not connecting to localhost anymore?

 

Coding isn't your strong suit, okay. Here's a modified function

function xml_stream_parser($socket, $callback, $limit = 0) {
	$depth = 0;  // node depth
	$xml = "";   // xml string buffer
	$count = -1; // ignore handshake

	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, &$count) {
				$xml .= $data;
				if ($data[0] == "<" && $data[1] == "/") {
					$depth--;
					if ($depth == 0) {
						$callback($xml);
						$count++;
					}
				} else if ($data[0] == "<") {
					$depth++;
				}
			});
		}

		// read until the parser did something
		// xml_parse natively supports a streaming approach
		do {
			$buffer = socket_read($socket, 1024);
			if ($buffer === false && socket_last_error($socket) != ???) {
				// read timeout
				continue;
			}
		} 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 != "" && (!$limit || $count < $limit));
}
header("Content-Type: application/json");

$s = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($s, "127.0.0.1", 10001);
socket_set_block($s); // block until data (or timeout)
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
	static $first = true;
	if ($first) {
		// handshake document... ignore?
		$first = false;
	} else {
		// detection document...
		$xml = simplexml_load_string($xml);
		echo json_encode([
			"key" => (string)$xml->foo->bar
		]);
	}
}, 1);
socket_shutdown($s);
socket_close($s);
Now you have to start learning or else find someone to hire (not me) to do the rest of it.

 

In that callback function in the small bit of code, it can process the handshake XML or the detection XML. I think you can ignore the handshake and instead focus on the detection. You want to open a popup for something, right? You need certain information from the XML to do that. Use PHP and SimpleXML(Element) to find that information, then plug it into that json_encode() thing.

 

When you have, go to that page in your browser and make a detection event happen. You should see something like

{"key":"something"}
as output that contains the information you need. Can you get that far?
Link to comment
Share on other sites

No. And I forgot to mention, those other errors sound like the server is down. You're not connecting to localhost anymore?

 

Coding isn't your strong suit, okay. Here's a modified function

function xml_stream_parser($socket, $callback, $limit = 0) {
	$depth = 0;  // node depth
	$xml = "";   // xml string buffer
	$count = -1; // ignore handshake

	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, &$count) {
				$xml .= $data;
				if ($data[0] == "<" && $data[1] == "/") {
					$depth--;
					if ($depth == 0) {
						$callback($xml);
						$count++;
					}
				} else if ($data[0] == "<") {
					$depth++;
				}
			});
		}

		// read until the parser did something
		// xml_parse natively supports a streaming approach
		do {
			$buffer = socket_read($socket, 1024);
			if ($buffer === false && socket_last_error($socket) != ???) {
				// read timeout
				continue;
			}
		} 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 != "" && (!$limit || $count < $limit));
}
header("Content-Type: application/json");

$s = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($s, "127.0.0.1", 10001);
socket_set_block($s); // block until data (or timeout)
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
	static $first = true;
	if ($first) {
		// handshake document... ignore?
		$first = false;
	} else {
		// detection document...
		$xml = simplexml_load_string($xml);
		echo json_encode([
			"key" => (string)$xml->foo->bar
		]);
	}
}, 1);
socket_shutdown($s);
socket_close($s);
Now you have to start learning or else find someone to hire (not me) to do the rest of it.

 

In that callback function in the small bit of code, it can process the handshake XML or the detection XML. I think you can ignore the handshake and instead focus on the detection. You want to open a popup for something, right? You need certain information from the XML to do that. Use PHP and SimpleXML(Element) to find that information, then plug it into that json_encode() thing.

 

When you have, go to that page in your browser and make a detection event happen. You should see something like

{"key":"something"}
as output that contains the information you need. Can you get that far?

 

Thank you! You have really tried. 

My server was not down actually, the code where we have this

{if ($buffer === false && socket_last_error($socket) != '73730') {

                // read timeout

                continue;

            }} ,

actually refused to load as it displays that error but when i used the one without the line of code, it will just echo out two XML and stop echoing anything again. And if i tried to reload it will just keep connecting without pushing out any information. What might caused that ?

 

I promise to start learning the hard way now, guess the pressure is the one telling on me.

 

I will try to test now this new code now, i can surely get that far to achieve it !

 

Thank you so much REQUINIX But atleast let me show a little appreciation even if it is a token, you are a true genius 

Link to comment
Share on other sites

THIS GUY REFUSES TO LOAD 

 

{

<?php
function xml_stream_parser($socket, $callback, $limit = 0) {
$depth = 0;  // node depth
$xml = "";   // xml string buffer
$count = -1; // ignore handshake
 
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, &$count) {
$xml .= $data;
if ($data[0] == "<" && $data[1] == "/") {
$depth--;
if ($depth == 0) {
$callback($xml);
$count++;
}
} 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);
if ($buffer === false && socket_last_error($socket) != '10072') {
// read timeout
continue;
}
} 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 != "" && (!$limit || $count < $limit));
}
 
header("Content-Type: application/json");
 
$s = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($s, "192.168.0.78", 10001);
socket_set_block($s); // block until data (or timeout)
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
static $first = true;
if ($first) {
// handshake document... ignore?
$first = false;
echo '1';
} else {
// detection document...
$xml = simplexml_load_string($xml);
echo json_encode([
"key" => (string)$xml->DeviceDetectionRecord->Detection->DetectionEvent
]);
}
}, 1);
socket_shutdown($s);
socket_close($s);
?>

}

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.