Jump to content

Receiving an xml doc over HTTP


kernelpanik
 Share

Recommended Posts

Hi all,there are two scripts involved in my problem. The first constructs xml and posts it using HTTP Request to my PHP script.Kinda like this I guess :

POST /InStock HTTP/1.1Host: www.example.orgContent-Type: application/soap+xml; charset=utf-8Content-Length: nnn<?xml version="1.0"?><soap:Envelopexmlns:soap="http://www.w3.org/2001/12/soap-envelope"soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">  <soap:Body xmlns:m="http://www.example.org/stock">    <m:GetStockPrice>      <m:StockName>IBM</m:StockName>    </m:GetStockPrice>  </soap:Body></soap:Envelope>

My script will then take the xml, fiddle with it, and send it back via the same way.What I dont get is how my PHP script retrieves the xml from the request in the first place.Can anyone clarify this for me?Thanks,kp.

Link to comment
Share on other sites

Maybe the post above is unclear....if you have a file named "soap.xml" that you want to parse..."soap.xml" is the location of the file you want to look at...in the same way, "php://input" is the location of the "raw POST data". Note that it is the location of the data. You don't need to worry about skipping the HTTP Header info, because it isn't there - only the data is.anyway... I don't know how you plan to work with the soap message, but the point is, its location will be "php://input"read more here (not much more though... actually... maybe it's less....)I would recommend you hard code the soap message to an xml file on the server for development. if you want just store the file name in a variable

$file_name = "soap.xml"

This will make testing/debugging much, much easier! You can print debug info to the browser and see where things are going wrong...Once your code works like you want, go ahead and change the value of $file_name to "php://input", remove echo statements that were used for debug and your done. You can then start calling your php script from your soap client, instead of just the browser

Link to comment
Share on other sites

Maybe the post above is unclear....if you have a file named "soap.xml" that you want to parse..."soap.xml" is the location of the file you want to look at...in the same way, "php://input" is the location of the "raw POST data". Note that it is the location of the data. You don't need to worry about skipping the HTTP Header info, because it isn't there - only the data is.anyway... I don't know how you plan to work with the soap message, but the point is, its location will be "php://input"read more here (not much more though... actually... maybe it's less....)I would recommend you hard code the soap message to an xml file on the server for development. if you want just store the file name in a variable
$file_name = "soap.xml"

This will make testing/debugging much, much easier! You can print debug info to the browser and see where things are going wrong...Once your code works like you want, go ahead and change the value of $file_name to "php://input", remove echo statements that were used for debug and your done. You can then start calling your php script from your soap client, instead of just the browser

Hi,thanks very much for that. I appreciate it. Gonna give it a go now, after ages of not knowing what to do :-)kp.
Link to comment
Share on other sites

Did you know you can check the request type too?The following code returns the request type (GET, PUT, POST, HEAD...)
$HTTP_SERVER_VARS['REQUEST_METHOD']

After getting your code to work on the xml like you want, you may want to have the output change depending on the request type. ex: if method is POST, return SOAP message. if method is GET show html that says the page doesn't support that method (unless you want to support get....), or check if the variable for get is "wsdl" and show your wsdl file...Just figured it is worth mentioning....

Link to comment
Share on other sites

Did you know you can check the request type too?The following code returns the request type (GET, PUT, POST, HEAD...)
$HTTP_SERVER_VARS['REQUEST_METHOD']

After getting your code to work on the xml like you want, you may want to have the output change depending on the request type. ex: if method is POST, return SOAP message. if method is GET show html that says the page doesn't support that method (unless you want to support get....), or check if the variable for get is "wsdl" and show your wsdl file...Just figured it is worth mentioning....

Hey,thanks for the info and help. The script is working in the sense that with my own XML, all is good and I can post back results via sockets. However, as soon as I try to change to php://input, it doesnt work.I am assuming that the xml is being posted correctly to me, as the company I deal with do this daily. So, it means that I'm doing something wrong.I just switch between these :
 //$fileContents = "<my xml assigned to variable>";$fileContents = file_get_contents("php://input");

I need to add logging now so that I can check what the story is, and then I need to start posting the information to my script myself, so that I can verify.It never ends :-)kp.

Link to comment
Share on other sites

Hey,thanks for the info and help. The script is working in the sense that with my own XML, all is good and I can post back results via sockets. However, as soon as I try to change to php://input, it doesnt work.I am assuming that the xml is being posted correctly to me, as the company I deal with do this daily. So, it means that I'm doing something wrong.I just switch between these :
 //$fileContents = "<my xml assigned to variable>";$fileContents = file_get_contents("php://input");

I need to add logging now so that I can check what the story is, and then I need to start posting the information to my script myself, so that I can verify.It never ends :-)kp.

Ok, all is working now - the xml format was slightly different to what was expected.Once I organise the code a little better, I will post here for others.kp.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...