xml = new DomDocument('1.0','utf-8'); $this->map = []; //['urlset',false,['xmlns'=>"http://www.sitemaps.org/schemas/sitemap/0.9"],[]]; $items = $mysql->select('SELECT * FROM content left join cat on cat.id = content.cat where cat.menu like ?','Y'); $sites = []; foreach($items as $sitem){ // mtime $item=[ // ['loc','http://'.$_SERVER['HTTP_HOST'].$sitem['path']], ['loc','https://mos80.com'.$sitem['path']], ]; if($sitem['mtime']){ preg_match('/^(\\d{4})-(\\d{2})-(\\d{2})( (\\d{2}):(\\d{2})(:(\\d{2}))?)?$/',$sitem['mtime'],$r); $mtime_w3c = date(DATE_W3C,mktime($r[5],$r[6],$r[8],$r[2],$r[3],$r[1])); $item[]=['lastmod',$mtime_w3c]; } $this->map[]=['url',false,false,$item]; } } function make_xml_element($xml,$name='tag',$value=false,$attr=false,$item=false,$cdata=false){ $element=$xml->createElement($name); if($value) $element->appendChild(($cdata)?$xml->createCDATASection($value):$xml->createTextNode($value)); if(is_array($attr)&&!empty($attr))foreach($attr as $k=>$v){$newAttr=$xml->createAttribute($k);$newAttr->value=$v;$element->appendChild($newAttr);} if(is_array($item)&&!empty($item))foreach($item as $v){$element->appendChild($this->make_xml_element($xml,$v[0],$v[1],$v[2],$v[3],$v[4]));} return $element; } function compile(){ $this->xml->appendChild($this->make_xml_element($this->xml,'urlset',false,['xmlns'=>'http://www.sitemaps.org/schemas/sitemap/0.9'],$this->map)); return $this->xml->saveXML(); } } $map = new sitemap(); $outerXML = $map->compile(); file_put_contents($_SERVER['DOCUMENT_ROOT'].'/tmp/sitemap.xml',$outerXML); echo $outerXML; ?>