views:

122

answers:

1

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();

+3  A: 
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.

VolkerK
yes it is my responsibility to store the file but that is being done by the curl using the code like $ch = curl_init($request); $fp = fopen($filename, "w"); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch);
gforg
ah ok, then use readfile() unless you want to process/alter the document.
VolkerK
thanks. Readfile worked and after your suggestion I did some searching and all points to using readfile for my scenario. Thanks!
gforg
It _could_ be that redirecting the client with a "302 temporary redirect" is also an option. Wouldn't be a transparent proxy though and requires an additional http round trip.
VolkerK
I don't think the objection to `file_get_contents` is valid in this case, since it it will use `nmap`: http://lxr.php.net/opengrok/xref/PHP_TRUNK/ext/standard/file.c#560
Artefacto
@Artefacto: I haven't dived deep into the code but from what I've seen it's file_get_contents -> _php_stream_copy_to_mem -> perealloc_rel_orig -> _erealloc ... If possible it uses mmap for reading the data but it's copied into heap memory. Did I miss something?
VolkerK
Artefacto
Artefacto
@Artefacto: Even if memory mapped files were used for reading the data, my critique is not about whether mmap is used or not but whether the _entire_ contents is kept in (heap) memory or not. And from what I see it looks like file_get_contents() always allocates heap memory and copies the entire contents in that memory, i.e. if you have a 200MB file your script's heap memory usage increases by 200MB.
VolkerK
Yes, you're right... The contents of the file were still copied to memory allocated in the heap... I thought it just returned the mmaped memory.
Artefacto
Now that I think about it, it wouldn't be possible to return the mmaped memory directly, since the zval could not indicate its encapsulated string was mmaped. So upon the destruction of the string, efree would be called on it, which wouldn't be very good.
Artefacto
And that's why it took until version 1.4 to introduce mmap to java ;-)
VolkerK