I am writing a proxy service for caching the queries that my mobile app makes to webservice. (like a man in the middle)
The task of proxy site I have built is pass the query it gets from the app onto third party webservice and save the response from the third party webservice as an XML file and for all subsequent calls for the same query read from the XML file and provide the response (basically caching the response -using Php, curl and simplexml_load_file).
Now my question is - What is the recommended way to read an xml file and return the string.
option 1: $contents = file_get_contents($filename); echo $contents;
option 2: $xml=simplexml_load_file($filename) echo $xml->asXML();
readfile($filename);
file_get_contents/echo first reads the entire contents into the php process' memory and then sends it to the output stream. It's not necessary to have the entire content in memory if all you want to do id to forward it.
simplexml_load_file() not only reads the entire content into memory, it also parses the document which takes additional time. Again unnecessary if you don't want to get specific data from the document or test/modify it.
readfile() sends the content directly to the output stream and can do so "any way it see's fit". I.e. if supported in can use memory mapped files, if not it can at least read the contents in smaller chunks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With