#!/usr/local/bin/php [[ 'version' => '2.0', 'xmlns:content' => 'http://purl.org/rss/1.0/modules/content/', 'xmlns:wfw' => 'http://wellformedweb.org/CommentAPI/', 'xmlns:dc' => 'http://purl.org/dc/elements/1.1/', 'xmlns:atom' => 'http://www.w3.org/2005/Atom', 'xmlns:sy' => 'http://purl.org/rss/1.0/modules/syndication/', 'xmlns:slash' => 'http://purl.org/rss/1.0/modules/slash/' ], 'channel' => [null, 'title'=> [null,'WIDE POST SPACE'], 'atom:link' => [['href' => 'https://widepost.space/feed/', 'rel' => 'self', 'type' => 'application/rss+xml' ]], 'item' => [null, 'title' => [null,'テスト'], 'link' => [null,'https://widepost.space/blog/2017/11/'] ] ] ] ]; public function __construct() { $this->dom = new DOMDocument('1.0', 'UTF-8'); $this->domAppend($this->dom, self::$rss_info_arr); } public function appendItem(){ //$this->dom->documentElement->appendChild($this->dom->createElement('item','アイテムの値')); $xpath = new DOMXPath($this->dom); $xpath->query('//channel')->item(0)->appendChild($this->dom->createElement('item','アイテムの値')); } public function saveDom() { header("Content-Type: text/xml"); return $this->dom->saveXML(); } private function domAppend($n, $ele_arr) { //global $dom; if(!is_array($ele_arr) || empty($ele_arr)){ return $n; } reset($ele_arr); $local_n = $n; foreach ($ele_arr as $key => $ele) { $value = null; if(is_array($ele) && !empty($ele[1]) && !is_array($ele[1])){ $value = strval($ele[1]); } $local_n = $n->appendChild($this->dom->createElement($key,$value)); $setAttr_val = null; if(is_array($ele) && $ele != null){ $setAttr_val = array_splice($ele, 0, 1); $local_n = $this->setAttr($local_n, $setAttr_val[0]); if(!empty(reset($ele)) && is_array(reset($ele)) ){ $local_n = $this->domAppend($local_n, $ele); } } } return $n; } private function setAttr($n, $attr_arr){ foreach ((array)$attr_arr as $key => $attr) { $n->setAttribute($key, $attr); } return $n; } } $rss = new MakeCustomRss(); $rss->appendItem(); $rss->appendItem(); $rss->appendItem(); $rss->appendItem(); echo $rss->saveDom(); ?>