Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中使用DOMDocument加速循环_Php_Arrays_Loops_Foreach_Domdocument - Fatal编程技术网

如何在PHP中使用DOMDocument加速循环

如何在PHP中使用DOMDocument加速循环,php,arrays,loops,foreach,domdocument,Php,Arrays,Loops,Foreach,Domdocument,我正在通过DOMDocument从外部xml文件加载25000个项目(每个站点地图5000个),在一个循环中循环5个站点地图大约需要15-20秒,这太多了 我很震惊,我在循环中做错了什么 你能检查一下代码,看是否有什么东西导致加载时间过长吗 我不知道 代码: $resultHTML=''; $sitemaps=[ '0' => 'http://example.com/sitemap_part1.xml', '1' => 'http://example.com/sitemap_part2.xml'

我正在通过DOMDocument从外部xml文件加载25000个项目(每个站点地图5000个),在一个循环中循环5个站点地图大约需要15-20秒,这太多了

我很震惊,我在循环中做错了什么

你能检查一下代码,看是否有什么东西导致加载时间过长吗

我不知道

代码:

$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($sm形式的站点地图):
$DomDocument=新的DomDocument();
$DomDocument->preserveWhiteSpace=false;
$DomDocument->load($sm);
$DomNodeList=$DomDocument->getElementsByTagName('loc');
foreach($DomNodeList作为$url):
//$i++;
$resultHTML.='';
$resultHTML.=$url->nodeValue;
$resultHTML.='';
endforeach;
endforeach;
echo$resultHTML;

这是一个未经测试的小文件缓存如何工作的示例。 您应该添加一些错误处理,但我认为它会起作用

更新:固定了
文件内容中的变量名($filepath,$resultHTML)

$resultHTML='';
$chacheDir=“缓存”//path/to/your/cachedir
$cacheTime=24*60*60;//24小时
$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($sm形式的站点地图):
$filepath=$chacheDir.'/'.md5($sm);
//检查缓存文件是否存在,以及是否已经太旧
如果(file_exists($filepath)&((time()-filemtime($filepath))preserveWhiteSpace=false;
//$DomDocument->load($sitemap\uURL);
$DomDocument->load($sm);
$DomNodeList=$DomDocument->getElementsByTagName('loc');
foreach($DomNodeList作为$url):
//$i++;
$resultHTML.='';
$resultHTML.=$url->nodeValue;
$resultHTML.='';
endforeach;
文件内容($filepath,$resultHTML);
}
endforeach;
echo$resultHTML;

缓存此结果如何?我认为站点地图不会每分钟都更改。您可以将其缓存几分钟/小时/days@swidmann说得对。你应该缓存它。一个简单的解决方案是。老实说,即使将它写入一个简单的文件并提供服务也可以。嗯,谢谢你们,你们是对的,它可能不是po我们可以使用一些PHP魔术来加快速度。谢谢,伙计,我稍后会测试它,如果有帮助的话,我会让你知道并勾选答案,现在谢谢。@JohnDoerthy,没问题,但是要注意:这是未经测试的,也许你会得到一些错误。
$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;
$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;