Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,756 members, 7,824,169 topics. Date: Saturday, 11 May 2024 at 02:16 AM

PHP Socket Programming To Send And Receive XML File Data - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / PHP Socket Programming To Send And Receive XML File Data (2296 Views)

Contest, Programming To Solve Real Life Problems (photo) / C++ Programming: To Do Or Not To Do. / Programming To Your Doorstep (2) (3) (4)

(1) (Reply) (Go Down)

PHP Socket Programming To Send And Receive XML File Data by rushman003: 9:18pm On Jul 01, 2017
Gurus in the house please i really need your help on this,
I'm trying to connect to an Alarm device on a network to receive xml data and process it to trigger another device,

Have got a clue from my boss to use php socket programming to connect to the device, but i'm finding it very hard to send and retrieve the XML data even if it is to output it in an array or dump in mysql server for now, i could only find example for HTTP request online but couldn't find for XML request,

Please my ogas i need your expertise

Here is my code for socket connect and XML data request/grabbing

//connector.php
<?php
//not timing out
set_time_limit(0);
//creating socket
$socket= socket_create(AF_INET, SOCK_STREAM,SOL_TCP);

if ((!$socket= socket_create(AF_INET,SOCK_STREAM,SOL_TCP))){
$errorcode= socket_last_error();
$errormsg= socket_strerror ($errorcode);

die("Could not create socket: [$errorcode] $errormsg \n"wink;

}
echo "I'm the Socket creator \n <br>";
//Creating a socket connections
if (!socket_connect($socket, '192.168.43.16', 80)){
$errorcode = socket_last_error();
$errormsg = socket_strerror ($errorcode);

die ("Could not estabish port connection: [$errorcode] $errormsg \n"wink;

}
echo "Port 1000 Connection Established \n <br>";

$message = "GET / HTTP/1.1\r\n\r\n";

//Send the message to the server
if( ! socket_send ( $socket , $message , strlen($message) , 0))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);

die("Could not send data: [$errorcode] $errormsg \n"wink;
}

echo "Message send successfully \n";

//Now receive reply from server
if(socket_recv ( $socket , $buf , 2045 , MSG_WAITALL ) === FALSE)
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);

die("Could not receive data: [$errorcode] $errormsg \n"wink;
}

//print the received message
echo $buf;
socket_close($socket);
?>


//dataextractor.php
<?php
include_once("connector.php"wink;
// Loading the XML file
$xml=simplexml_load_file("intrusion.xml"wink or die("Error: Cannot create object"wink;
echo $xml->DeviceIdentification[0]-> DeviceName ->DeviceType . "<br>";
echo $xml->Detention[1]->ID -> DetentionEvent;

?>

For the dataextractor the echo statement are the values i need to process to trigger the other device from the XML file

Thanks in advance

All thanks to my boss talk2hb1 for his painstaking effort, i'm doing this to put myself through learning process tho, because he's working on this with me and asked me to read about it...... Watch out for an article from me dedicated to him//
Re: PHP Socket Programming To Send And Receive XML File Data by melodyogonna(m): 10:16pm On Jul 01, 2017
I can't help you but here is a piece of advice.


I 'd advise you to ask this question on a programming forum as nairaland has clearly proven not to be one, you should get answers faster on 'dream in code', 'code project' and 'stackoverflow' than nairaland.
Re: PHP Socket Programming To Send And Receive XML File Data by Nobody: 11:10pm On Jul 01, 2017
rushman003:
Gurus in the house please i really need your help on this,
I'm trying to connect to an Alarm device on a network to receive xml data and process it to trigger another device,

Have got a clue from my boss to use php socket programming to connect to the device, but i'm finding it very hard to send and retrieve the XML data even if it is to o

Why would you ever want to build something that incorporates with a security platform from scratch?

First use the RacthetPHP (http://socketo.me) library for your socket server implementation if you fancy security... then must you send the data in XML? Why not try a JSON or PHP array that can be decoded on the Alarm then processed to trigger the device programmatically....

I suggest you use CodeIgniter as a framework cos this is no small project and you'd need to expand, then download the RatchetPHP or any socket library for CodeIgniter, next get an API library for CodeIgniter and build an API that responds with XML data when called. If you're doing this manually you have to set the header type to XML for the device to pick the data up... Recommended this step for a white dude who wanted to build a remote door controller... with a Raspberry Pi and it worked out...before I say much more, this alarm what platform does it run on? Mobile or an Embedded system such as the Pi?
Re: PHP Socket Programming To Send And Receive XML File Data by rushman003: 7:37am On Jul 02, 2017
DanielTheGeek:


Why would you ever want to build something that incorporates with a security platform from scratch?

First use the RacthetPHP (http://socketo.me) library for your socket server implementation if you fancy security... then must you send the data in XML? Why not try a JSON or PHP array that can be decoded on the Alarm then processed to trigger the device programmatically....

I suggest you use CodeIgniter as a framework cos this is no small project and you'd need to expand, then download the RatchetPHP or any socket library for CodeIgniter, next get an API library for CodeIgniter and build an API that responds with XML data when called. If you're doing this manually you have to set the header type to XML for the device to pick the data up... Recommended this step for a white dude who wanted to build a remote door controller... with a Raspberry Pi and it worked out...before I say much more, this alarm what platform does it run on? Mobile or an Embedded system such as the Pi?

Thank you Dan
Lemme explain how ds work in full, the alarm device communicate in XML format to the network to push out data for third party communication. When a signal is being transmitted from the perimeter it secures the ASCII format is being processed to text in XML format to tell the communicating head end equipment that so and so zone is having an intrusion. I don't know the technology use to make the device but software related part was written in C#. They have SDK you can work with but I only need the alarm output data to trigger a camera attached to the zone for operator to view what's happening there
Re: PHP Socket Programming To Send And Receive XML File Data by micodon(m): 9:34am On Jul 02, 2017
I really don't understand what you want. If there's an xml file that's sitting somewhere on the network, use file_get_contents

I mean..if the file is at 10.1.1.1/file.xml, then

<?php
$content = file_get_contents("10.1.1.1/file.xml" );
echo $content;
?>
Re: PHP Socket Programming To Send And Receive XML File Data by rushman003: 6:05am On Jul 03, 2017
micodon:
I really don't understand what you want. If there's an xml file that's sitting somewhere on the network, use file_get_contents

I mean..if the file is at 10.1.1.1/file.xml, then

<?php
$content = file_get_contents("10.1.1.1/file.xml" );
echo $content;
?>
Thank you micodon
What I wanna achieve is that I have a network device that output some XML data to the network. But you can't get those data until you do some sort of handshake with and request for this XML data

Hope you understand now
Re: PHP Socket Programming To Send And Receive XML File Data by stanliwise(m): 10:34pm On Jul 09, 2022
rushman003:

Thank you micodon
What I wanna achieve is that I have a network device that output some XML data to the network. But you can't get those data until you do some sort of handshake with and request for this XML data

Hope you understand now
You later achieved this?

(1) (Reply)

Study Group For C++ Learner / Lagos Github Meetup / How Can I Download From Pirate Bay?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 26
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.