我想创建一些XML,基本上通过阅读rss提要,并添加到他们一些自定义标签。我做了一个包含我的代码的函数,现在我想用不同的rss url来多次调用函数。每次调用都会产生一个不同的.xml文件。
我使用DOMDocument加载和解析rss,并且simple_html_dom加载和解析每个rss项目的链接以从html获取一些内容。
这里是我的代码一个简单的例子:PHP等到上一个函数完成
<?php
include('simple_html_dom.php');
load_custom_rss('http://www.somesite.com/rssfeed/articles', 'articles.xml');
load_custom_rss('http://www.somesite.com/rssfeed/jobs', 'jobs.xml');
load_custom_rss('http://www.somesite.com/rssfeed/press', 'press.xml');
//up to 20 similar function calls here...
function load_custom_rss($link, $filename){
$doc = new DOMDocument();
$doc->load($link);
$newDoc = new DOMDocument('1.0', 'UTF-8');
$rss = $newDoc->createElement('rss');
$channel = $newDoc->createElement('channel');
$newDoc->appendChild($rss);
$rss->appendChild($channel);
foreach ($doc->getElementsByTagName('item') as $node) {
//here is some code to read items from rss xml/write them to new xml document.
//Code missing for simplicity
//Next lines used to get some elements from the html of the item's link
$html = new simple_html_dom();
html->load_file($node->getElementsByTagName('link')->item(0)->nodeValue);
$ret = $html->find('#imgId');
}
$newDoc->formatOutput = true;
$fh = fopen($filename, 'w') or die("can't open file");
fwrite($fh, $newDoc->saveXML());
fclose($fh);
unset($doc);
//unset ALL variables and objects created in this function...
//........
}//function end
?>
我的问题是,该函数的每个调用消耗的内存相当的量,所以之后的第三或功能的Apache 4调用抛出致命错误,如该脚本消耗的内存量大于memory_limit,即使我未设置该函数中创建的所有变量和对象。如果我减少1或2的函数调用一切正常。
有什么办法可以工作?我正在考虑每个函数调用在启动之前等待前一个完成,但是怎么做呢?
希望有人能帮助。 在此先感谢。
这是否必须从apache运行,还是可以从命令行运行?如果是后者,则对时间和记忆没有如此严格的限制。 – 2011-05-02 12:28:03