title: Extract all url from sitemap.xml with PHP CURL
body: <p>I want to extract all url from sitemap.xml with PHP and CURL. My code work with content sitemap (e.g: <a href="http://www.phanmemtoday.com/sitemap.xml?page=1" rel="nofollow">http://www.phanmemtoday.com/sitemap.xml?page=1</a>) but does not work with sitemap index (e.g: <a href="http://www.phanmemtoday.com/sitemap.xml" rel="nofollow">http://www.phanmemtoday.com/sitemap.xml</a>).
  Please help me. Thank you!</p>
  
  <pre><code>&lt;?php
  $sUrl="http://domain.com/sitemap.xml";
  
  $aXmlLinks = array($sUrl);
  $aOtherLinks = array();
  while (!empty($aXmlLinks)) {
      foreach ($aXmlLinks as $i =&gt;$sTmpUrl){
          unset($aXmlLinks[$i]);
          $aTmp = getlinkfromxmlsitemap($sTmpUrl);
          echo "Array temp link:&lt;br&gt;";
          print_r($aTmp);
          foreach ($aTmp as $sTmpUrl2) {
              if (strpos($sTmpUrl2, '.xml') !== false) {
                  array_push($aXmlLinks,$sTmpUrl2);
              } else {
                  array_push($aOtherLinks,$sTmpUrl2);
              }
          }
      }
      echo "&lt;br&gt;Array xml link:&lt;br&gt;";
      print_r($aXmlLinks);
      echo "&lt;br&gt;Array product link:&lt;br&gt;";
      print_r($aOtherLinks);
      echo "&lt;br&gt;-----------------------------------------&lt;br&gt;";
  }
  print_r($aOtherLinks);
  
  
  function getlinkfromxmlsitemap($sUrl) {
      // echo "Get link from: $sUrl&lt;br&gt;";
      $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('@&lt;loc&gt;(.+?)&lt;\/loc&gt;@', $data, $matches);
      for ($i = 0; $i &lt; $count; ++$i) {
          $links[] = $matches[0][$i];
      }
      return $links;  
  }
  ?&gt;
  </code></pre>