PHP:XML 文件到字符串,使用 asXML() 更快 file_get_contents 或 simplexml_load_file [英] PHP: XML file to string which is faster file_get_contents or simplexml_load_file with asXML()

查看:22
本文介绍了PHP:XML 文件到字符串,使用 asXML() 更快 file_get_contents 或 simplexml_load_file的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个代理服务,用于缓存我的移动应用程序对网络服务的查询.(就像中间的男人)

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

我构建的代理站点的任务是将它从应用程序获取的查询传递给第三方 Web 服务,并将来自第三方 Web 服务的响应保存为 XML 文件,并用于从 XML 读取的相同查询的所有后续调用文件并提供响应(基本上缓存响应 - 使用 Php、curl 和 simplexml_load_file).

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 文件并返回字符串的推荐方法是什么.

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

选项 1:$contents = file_get_contents($filename);回声 $contents;

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

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

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

推荐答案

readfile($filename);

<小时>

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


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() 将内容直接发送到输出流,并且可以以任何方式这样做"看合适".IE.如果支持可以使用内存映射文件,如果不支持它至少可以读取较小块的内容.

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.

这篇关于PHP:XML 文件到字符串,使用 asXML() 更快 file_get_contents 或 simplexml_load_file的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆