.Maui Blog

How to parse an xml sitemap using PHP and Curl

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.example.com/sitemap.xml');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36');
$resp = curl_exec ($ch);

$xml = new SimpleXMLElement($resp);

foreach ($xml->url as $url_list) {

    $url = $url_list->loc;
    echo $url . "<br />";

}

curl_close ($ch);
Exit mobile version