[英]How to speed up loop using DOMDocument in PHP
我通过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 ( $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;
这是一个未经测试的示例,说明小型文件缓存如何工作。 您应该添加一些错误处理,但是我认为它将起作用。
更新:在file_put_contents( $filepath, $resultHTML );
处的固定变量名称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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.