views:

304

answers:

2

Looking to read the file size of the SOAP POST, any best practices?

$data = fopen('php://input','rb');
$content = fread($data,5000);

$dom = new DOMDocument();
$dom->loadXML($content);

Would like the 5000 to be dynamic as each SOAP POST size will be different or does this matter?

Using fread() would be great

+2  A: 

You could try the following instead:

$xml = file_get_contents('php://input')

This will get all contents, no matter the length of the data.

Lucas Oman
I was using this but I couldn't get it to work with DOMDocument() and load the XML
Phill Pafford
That's odd. I haven't used it with DOMDocument, but I've used it with SimpleXMLElement, and it works just fine.
Lucas Oman
I also tried SimpleXML with no luck as to what I wanted to do. The code I have works just wanted to know how I could make the size dynamic instead of hard coding it. Thanks for the help
Phill Pafford
It may be that your fopen wrappers (http://us3.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen) are disabled. I'm not sure that it would affect php:// URLs, but it may.
Lucas Oman
+2  A: 

Umm. If you can read it with 'fread', I see no reason you cannot read EXACT same text with 'file_get_contents()'. I use this a few times and remembering that I've try both.

As far as the best practice for 'fread' goes, what you need is the file size which you can get it from getallheaders().

So, if you still prefer using 'fread' here is the code.

$data = fopen('php://input','rb');
$Headers = getallheaders();
$CLength  = $Headers['Content-Length'];
$content = fread($data,$CLength);

$dom = new DOMDocument();
$dom->loadXML($content);

The code above is self explained so there is no need for further explanation. Just a little bit note that if the length is longer than 8192 bytes the content will be clipped. So you better check the read length to see if it clipped. (You will not need to be worry if you use 'file_get_contents()' though).

Hope this helps

NawaMan
Thanks I think this will do it :) Cheers
Phill Pafford