首先,我建议您使用cURL。无论您的 XML 有多大,内存问题都会减少。
$fp = fopen('/var/www/vhosts/my.com/xml/feed.xml', 'w'); // opening file handler to write feed in
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://domain.com/xml/page.xml'); // setting URL to take XML from
curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); // If result is gziped
curl_setopt($ch, CURLOPT_SSLVERSION, 3); // OpenSSL issue
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // Wildcard certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); // disabling buffer output, bec. we want to write XML to the file first and don't need it to be returned into variable
curl_setopt($ch, CURLOPT_FILE, $fp); // here we should transfer opened file handler to the cURL and it should be writable!
$result = curl_exec($ch); // executing download
$reponse_code = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); // retrieving HTTP return code for our request. Was it successful or not.
因此,即使 XML 提要在 SSL 和 GZIPed 之后,您也可以直接下载/保存到文件中。
使用curl_getinfo(),您可以获得有关您的请求的各种信息。如果程序应该是自动化的,那么如果请求失败,最好决定要做什么。
然后,如果文件不大(我的意思是超过 200 - 300 Mb 的非常大的文件),您可以使用 SimpleXML(仅从 PHP5 开始提供)库并解析您的数据。如果您在 PHP4 下(今天仍然可以)尝试查找 libXML,这也非常有用。
如果您检索的文件相当大:) 具有FILE 权限的 MySQL 数据库是您的朋友。