CdB
CdB

Reputation: 4908

php wait till previous function has finished

I'm trying to create some xml , basically by reading rss feeds and adding to them some custom tags. I've made a function that contains my code, and now i want to call the function several times with different rss urls. Each call will produce a different .xml file.
I use DOMDocument to load and parse the rss, and simple_html_dom to load and parse the link of each rss item to get some content from the html.
Here is a simplified example of my code:

<?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
?>  

My problem is that each call of the function consumes quite an amount of memory, so after the 3rd or 4th call of the function apache throws Fatal Error, as the script consumes memory amount bigger than the memory_limit, even though i unset ALL variables and objects created in the function. If i reduce the function calls to 1 or 2 everything works fine.
Is there any way it could work? I was thinking about each function call waits for the previous to finish before starting, but how could this be done?

Hope somebody could help. thanks in advance.

Upvotes: 3

Views: 9736

Answers (2)

Oliver Emberton
Oliver Emberton

Reputation: 951

You're unsetting $doc but not $newDoc, try adding

unset($newDoc);

At the end of that function.

As others have said, the problem is you're leaking memory or exceeding your memory limit; this is nothing to do with waiting until previous code has finished.

Alternatively you could put each call to load_custom_rss() into separate requests, so the script calls one and then reloads itself, i.e.

$i = $_GET['i'];

if ($i==0)
    load_custom_rss('http://www.somesite.com/rssfeed/articles', 'articles.xml'); 
elseif ($i==1)
    load_custom_rss('http://www.somesite.com/rssfeed/jobs', 'jobs.xml');

... etc ...

else
    die("I'm done");

header("Location: myself.php?i=".($i+1));

Your approach to reloading the script would likely be different of course, depending on whether the page needs to render any HTML first.

Upvotes: 1

Fidi
Fidi

Reputation: 5834

The thing you want is normal behaviour in php. It's worked through from top to bottom. Each function has to wait, till the previous function has finished. I think you problem is rather the memory-limit within the php.ini. Open the file and search for the directive: memory_limit http://www.php.net/manual/en/ini.core.php#ini.memory-limit Increase it to fit your needs.

Upvotes: 2

Related Questions