PHP 脚本,用于从特定 URL 加载和缓存 XML 数据库的副本


PHP script to Load and Cache a copy of XML database from specific URL

我有一个数据库,它延迟了很多在 xml 中加载,假设 URL 是 www.mysite.com/my-database/的,我需要一个 PHP 脚本来加载此 URL 并在加载后保存内容的副本(加载数据库需要 120 多秒),并将其副本存储在我网站的根目录中, 名称为 db.xml。感谢您的帮助。

我正在发送答案

基本上你所要做的就是将页面信息存储在一个变量中,然后设置位置并将变量作为要保存到您设置的位置的信息,因此:

file_put_contents('/the-full-server-path/to-where/you-want-the-file-saved/file.ext',$the_variable_you_set_with_the_page_info);

如果您无法弄清楚如何将页面信息存储在创建文件的脚本中的变量中,则可以使用 PHP 创建一个简单的打开/保存文件。

<?php
# This will open the actual xml page on your site with
# PHP and store the info in a variable. [Obviously you
# Have to change the location to the URL of the actual
# XML page people view.]
$the_variable_you_set_with_the_page_info=file_get_contents('http://www.yoursite.com/your-xml-file-to-save.xml');
 # Then you can save the file info where ever you would like.
 # For this one you'll have to find your server path which is
 # usually something like /home/public_html/the-directory-to-use
file_put_contents('/the-full-server-path/to-where/you-want-the-file-saved/file.ext',$the_variable_you_set_with_the_page_info);
 ?>