PHP: XML file to a string that is faster than file_get_contents or simplexml_load_file with asXML ()

I am writing a proxy service to cache requests that my mobile application makes for a web service. (like a man in the middle)

The task of the proxy site that I created is to transfer the request that it receives from the application to a third-party web service and save the response from the third-party web service as an XML file and for all subsequent calls for the same request read from XML file and provide the response (basically caching the response - using Php, curl and simplexml_load_file).

Now my question . What is the recommended way to read an XML file and return a string.

option 1: $ contents = file_get_contents ($ filename); echo $ contents;

option 2: $ XML = simplexml_load_file ($ filename) echo $ xml-> asXML ();

+6
xml php web-services simplexml
source share
1 answer
readfile($filename); 

file_get_contents / echo first reads all the contents into the memory of the php process and then sends it to the output stream. There is no need to have all the content in memory, if all you want to do is id, forward it.
simplexml_load_file () not only reads all the content into memory, but also analyzes a document that takes extra time. Once again, if you do not want to receive specific data from a document or test / modify it.

readfile () sends the content directly to the output stream and can do this "in any way it sees fit." That is, if they are supported, they can use memory-mapped files if they cannot at least read the contents in smaller fragments.

+4
source share

All Articles