使用PHP CURL从sitemap.xml提取所有url

使用PHP CURL从sitemap.xml提取所有url,php,loops,curl,sitemap,Php,Loops,Curl,Sitemap,我想用PHP和CURL从sitemap.xml中提取所有url。我的代码适用于内容站点地图(例如:),但不适用于站点地图索引(例如:)。 请帮帮我。谢谢大家! <?php $sUrl="http://domain.com/sitemap.xml"; $aXmlLinks = array($sUrl); $aOtherLinks = array(); while (!empty($aXmlLinks)) { foreach ($aXmlLinks as $i =>$sTmpU

我想用PHP和CURL从sitemap.xml中提取所有url。我的代码适用于内容站点地图(例如:),但不适用于站点地图索引(例如:)。 请帮帮我。谢谢大家!

<?php
$sUrl="http://domain.com/sitemap.xml";

$aXmlLinks = array($sUrl);
$aOtherLinks = array();
while (!empty($aXmlLinks)) {
    foreach ($aXmlLinks as $i =>$sTmpUrl){
        unset($aXmlLinks[$i]);
        $aTmp = getlinkfromxmlsitemap($sTmpUrl);
        echo "Array temp link:<br>";
        print_r($aTmp);
        foreach ($aTmp as $sTmpUrl2) {
            if (strpos($sTmpUrl2, '.xml') !== false) {
                array_push($aXmlLinks,$sTmpUrl2);
            } else {
                array_push($aOtherLinks,$sTmpUrl2);
            }
        }
    }
    echo "<br>Array xml link:<br>";
    print_r($aXmlLinks);
    echo "<br>Array product link:<br>";
    print_r($aOtherLinks);
    echo "<br>-----------------------------------------<br>";
}
print_r($aOtherLinks);


function getlinkfromxmlsitemap($sUrl) {
    // echo "Get link from: $sUrl<br>";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0");
    curl_setopt($ch, CURLOPT_URL, $sUrl);
    $data = curl_exec($ch);
    curl_close($ch);
    $links = array();
    $count = preg_match_all('@<loc>(.+?)<\/loc>@', $data, $matches);
    for ($i = 0; $i < $count; ++$i) {
        $links[] = $matches[0][$i];
    }
    return $links;  
}
?>
$stmpur){
未设置($aXmlLinks[$i]);
$aTmp=getlinkfromxmlsitemap($sTmpUrl);
回显“阵列温度链接:
”; 打印(aTmp); foreach($aTmp作为$sTmpUrl2){ if(strpos($sTmpUrl2,'.xml')!==false){ 阵列推送($aXmlLinks,$sTmpUrl2); }否则{ 阵列推送($aOtherLinks,$sTmpUrl2); } } } echo“
数组xml链接:
”; 打印(aXmlLinks); echo“
阵列产品链接:
”; 打印(其他链接); 回声“
---------------------------------------
”; } 打印(其他链接); 函数getlinkfromxmlsitemap($sUrl){ //echo“获取链接自:$sUrl
”; $ch=curl_init(); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_USERAGENT,“Mozilla/5.0(Windows NT 6.3;WOW64;rv:47.0)Gecko/20100101 Firefox/47.0”); curl_setopt($ch,CURLOPT_URL,$sUrl); $data=curl\u exec($ch); 卷曲关闭($ch); $links=array(); $count=preg_match_all('@(+?)@',$data,$matches); 对于($i=0;$i<$count;++$i){ $links[]=$matches[0][$i]; } 返回$links; } ?>
您的代码运行良好,但您可以改进某些方面。请检查以下示例,塔尔将返回一个嵌套数组,其中包含您要查看的链接:

<?php
$sUrl1="http://www.phanmemtoday.com/sitemap.xml?page=1";
$sUrl2="http://www.phanmemtoday.com/sitemap.xml";

$aXmlLinks = array($sUrl1,$sUrl2);
$aOtherLinks = array();
while (!empty($aXmlLinks)) {
    foreach ($aXmlLinks as $i =>$sTmpUrl){
        unset($aXmlLinks[$i]);
        $aTmp = getlinkfromxmlsitemap($sTmpUrl);        
        array_push($aOtherLinks,$aTmp);        
    }
    echo "<br>Array xml link:<br>";
    print_r($aXmlLinks);
    echo "<br>Array product link:<br>";
    print_r($aOtherLinks);
    echo "<br>-----------------------------------------<br>";
}
print_r($aOtherLinks);


function getlinkfromxmlsitemap($sUrl) {
    // echo "Get link from: $sUrl<br>";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0");
    curl_setopt($ch, CURLOPT_URL, $sUrl);
    $data = curl_exec($ch);
    $error= curl_error($ch);
    curl_close($ch);
    $links = array();
    $count = preg_match_all('@<loc>(.+?)<\/loc>@', $data, $matches);
    for ($i = 0; $i < $count; ++$i) {
        $links[] = $matches[0][$i];
    }
    return $links;  
}
?>
$stmpur){
未设置($aXmlLinks[$i]);
$aTmp=getlinkfromxmlsitemap($sTmpUrl);
阵列推送($aOtherLinks,$aTmp);
}
echo“
数组xml链接:
”; 打印(aXmlLinks); echo“
阵列产品链接:
”; 打印(其他链接); 回声“
---------------------------------------
”; } 打印(其他链接); 函数getlinkfromxmlsitemap($sUrl){ //echo“获取链接自:$sUrl
”; $ch=curl_init(); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_USERAGENT,“Mozilla/5.0(Windows NT 6.3;WOW64;rv:47.0)Gecko/20100101 Firefox/47.0”); curl_setopt($ch,CURLOPT_URL,$sUrl); $data=curl\u exec($ch); $error=curl\u error($ch); 卷曲关闭($ch); $links=array(); $count=preg_match_all('@(+?)@',$data,$matches); 对于($i=0;$i<$count;++$i){ $links[]=$matches[0][$i]; } 返回$links; } ?>
它工作不正常。我想编写一个脚本,从站点地图索引中捕获所有子站点地图,然后从child-sitemap中捕获所有其他链接。sitemap.xml几乎没有子站点地图链接。我想捕获所有子站点地图链接,然后从中提取所有页面链接