PHP: XML文件到字符串,更快的file_get_contents或simplexml_load_file与asXML()

时间:2022-01-03 00:13:06

I am writing a proxy service for caching the queries that my mobile app makes to webservice. (like a man in the middle)

我正在编写一个代理服务来缓存我的移动应用对webservice的查询。(像中间的男人)

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).

代理网站我已经建立的任务是通过查询它从应用程序到第三方网络服务并保存来自第三方网络服务的响应是一个XML文件,后续要求相同的查询从XML文件中读取,并提供响应(基本上缓存响应—使用Php,旋度和simplexml_load_file)。

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

现在我的问题是:建议如何读取xml文件并返回字符串。

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

选项1:$contents = file_get_contents($filename);echo $内容;

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

选项2:$xml=simplexml_load_file($文件名)echo $xml->asXML();

1 个解决方案

#1


4  

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.

file_get_contents/echo首先将整个内容读入php进程的内存,然后将其发送到输出流。如果想要将整个内容转发到id中,则不需要将整个内容保存在内存中。simplexml_load_file()不仅将整个内容读入内存,还会解析需要额外时间的文档。如果您不想从文档中获取特定的数据或测试/修改它,那么这也是不必要的。

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.

readfile()将内容直接发送到输出流,并可以“以任何它认为合适的方式”这样做。例如,如果支持in,则可以使用内存映射文件,如果不支持,则至少可以以较小的块读取内容。

#1


4  

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.

file_get_contents/echo首先将整个内容读入php进程的内存,然后将其发送到输出流。如果想要将整个内容转发到id中,则不需要将整个内容保存在内存中。simplexml_load_file()不仅将整个内容读入内存,还会解析需要额外时间的文档。如果您不想从文档中获取特定的数据或测试/修改它,那么这也是不必要的。

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.

readfile()将内容直接发送到输出流,并可以“以任何它认为合适的方式”这样做。例如,如果支持in,则可以使用内存映射文件,如果不支持,则至少可以以较小的块读取内容。