问题
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
回答1:
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
回答2:
You could try the following instead:
$xml = file_get_contents('php://input')
This will get all contents, no matter the length of the data.
来源:https://stackoverflow.com/questions/1405457/php-soap-fread-dynamic-post-size