PHP SOAP fread() dynamic POST size

2019-08-05 08:58发布

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条回答
老娘就宠你
2楼-- · 2019-08-05 09:09

You could try the following instead:

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

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

查看更多
我命由我不由天
3楼-- · 2019-08-05 09:11

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

查看更多
登录 后发表回答