So I’ve been strugling with this for two days now with not a whole lot of success.
What I’m getting is an http POST generated by a webpage that is being sent to a php file that I’m working on creating on my server. So basically I’m trying to create a php listener script that when an http post occurs it will run the script save the xml data into a SimpleXMLElement so that I can write a MYSQL query to import the data into our database. Once I get the data into a SimpleXMLElement, I can take over from there, the issue i’m having is that I cannot get the listener.php file to listen to the HTTP POST and read the data into a local file.
<?php
public void RunServer()
{
var prefix = "http://*:4333/";
HttpListener listener = new HttpListener();
listener.Prefixes.Add(prefix);
try
{
listener.Start();
}
catch (HttpListenerException hlex)
{
return;
}
while (listener.IsListening)
{
var context = listener.GetContext();
ProcessRequest(context);
}
listener.Close();
}
private void ProcessRequest(HttpListenerContext context)
{
// Get the data from the HTTP stream
var body = new StreamReader(context.Request.InputStream).ReadToEnd();
$data = body
byte[] b = Encoding.UTF8.GetBytes("ACK");
context.Response.StatusCode = 200;
context.Response.KeepAlive = false;
context.Response.ContentLength64 = b.Length;
var output = context.Response.OutputStream;
output.Write(b, 0, b.Length);
context.Response.Close();
}
?>
This code is the closest I’ve gotten to what I need and I get a 500 error when I try to access the page. I’m not going to lie I’m pretty new to php and am having issues understanding function definitions. (Also I did get this code from a StackOverflow ticket that was closed)
I’m currently using php5 and apache on my server for my web hosting. ]
I have a php.info() file setup and can post it if need be.
If you want to save data into a SimpleXMLElement that will do it: