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