PHP SOAP fread() dynamic POST size

谁说胖子不能爱 提交于 2019-12-24 06:47:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!