简体   繁体   中英

How to speed up loop using DOMDocument in PHP

I am loading 25000 items (5000 per one sitemap) via DOMDocument from external xml files and it takes about 15 - 20 seconds to loop through the 5 sitemaps in a loop and that 'sa lot

I am affraid I am doing something wrong in the loop.

Could you check the code if there is something that is causing the loading to take that long?

I have no clue.

Code:

$resultHTML = '';

$sitemaps = [
    '0' => 'http://example.com/sitemap_part1.xml',
    '1' => 'http://example.com/sitemap_part2.xml',
    '2' => 'http://example.com/sitemap_part3.xml',
    '3' => 'http://example.com/sitemap_part4.xml',
    '4' => 'http://example.com/sitemap_part5.xml',
];

foreach ( $sitemaps as $sm ) :

        $DomDocument = new DOMDocument();
        $DomDocument->preserveWhiteSpace = false;
        $DomDocument->load($sm);
        $DomNodeList = $DomDocument->getElementsByTagName('loc');

        foreach($DomNodeList as $url) : 

            //$i++;

            $resultHTML .= '<div class="xml-item">';  
                $resultHTML .= $url->nodeValue;
            $resultHTML .= '</div>';

        endforeach;

endforeach;

echo $resultHTML;

This is an untested example how a small file cache could work. You should add some error handling, but I think it will work.

UPDATED: fixed variable names at file_put_contents( $filepath, $resultHTML );

$resultHTML = '';

$chacheDir = "cache";// path/to/your/cachedir
$cacheTime = 24 * 60 * 60;// 24 hours

$sitemaps = [
    '0' => 'http://example.com/sitemap_part1.xml',
    '1' => 'http://example.com/sitemap_part2.xml',
    '2' => 'http://example.com/sitemap_part3.xml',
    '3' => 'http://example.com/sitemap_part4.xml',
    '4' => 'http://example.com/sitemap_part5.xml',
];

foreach ( $sitemaps as $sm ) :
    $filepath = $chacheDir.'/'.md5( $sm );

    // check if cached file exists, and if it's too old already
    if( file_exists( $filepath ) && ( ( time() - filemtime( $filepath ) ) <= $cacheTime ) ) {
        // read from cache
        $resultHTML .= file_get_contents( $filepath );
    } else {
        //create cache file
        $DomDocument = new DOMDocument();
        $DomDocument->preserveWhiteSpace = false;
        //$DomDocument->load($sitemap_url);
        $DomDocument->load( $sm );
        $DomNodeList = $DomDocument->getElementsByTagName( 'loc' );

        foreach ( $DomNodeList as $url ) :

            //$i++;

            $resultHTML .= '<div class="xml-item">';
            $resultHTML .= $url->nodeValue;
            $resultHTML .= '</div>';

        endforeach;
        file_put_contents( $filepath, $resultHTML );
    }

endforeach;

echo $resultHTML;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM