在 DomDocument 中创建 <media:thumbnail> 命名空间

Create <media:thumbnail> Namespace in DomDocument

我正在尝试将 < media:thumbnail> 添加到我的 RSS Feed,但在添加 URL、WIDTH、HEIGHT 等属性和删除结束标记 < /media:thumbnail>。代码来源来自 here and I edited for my needs and the is to be added like explained here.

if($results){ //we have records
    while($row = $results->fetch_object()) //loop through each row
    {
        $item_node = $channel_node->appendChild($xml->createElement("item")); //create a new node called "item"
        $title_node = $item_node->appendChild($xml->createElement("title", $row->cName)); //Add Title under "item"
        $link_node = $item_node->appendChild($xml->createElement("link", "http://www.your-site.com/link/goes/here/")); //add link node under "item"

        $root = $xml->createElementNS('image.png', 'media:thumbnail');
        $root = $xml->appendChild($root);

        //Unique identifier for the item (GUID)
        $guid_link = $xml->createElement("guid", "http://www.your-site.com/link/goes/here/". $row->id);  
        $guid_link->setAttribute("isPermaLink","false");
        $guid_node = $item_node->appendChild($guid_link);

    }
}

输出:

<media:thumbnail xmlns:media="image.png"/>
<media:thumbnail xmlns:media="image.png"/>
<media:thumbnail xmlns:media="image.png"/>
<media:thumbnail xmlns:media="image.png"/>
<media:thumbnail xmlns:media="image.png"/>
<media:thumbnail xmlns:media="image.png"/>

输出应该如下所示,但我不能做更多,因为启动已经不工作了。

<media:thumbnail url="http://www.foo.com/keyframe.jpg" width="75" height="50" time="12:05:01.123" />

确保您已将允许 media:thumbnail 个节点的名称空间属性添加到 RSS 文档:

$rss_node->setAttribute("xmlns:media","http://search.yahoo.com/mrss/")

此处假设您使用变量名称“$rss_node”保存调用 appendChild 将 rss 数据附加到 xml 文档的结果。

然后处理从您的数据库中提取的数据行中的缩略图。替换为:

$root = $xml->createElementNS('image.png', 'media:thumbnail');
$root = $xml->appendChild($root);

有了这个:

$thumbnail_node = $xml->createElement("media:thumbnail");
$thumbnail_node->setAttribute( "URL", "http://www.foo.com/keyframe.jpg");
$thumbnail_node->setAttribute( "width", "75");
$thumbnail_node->setAttribute( "height", "75" );
$thumbnail_node->setAttribute( "time", "12:05:01.123");
$thumbnail_node = $item_node->appendChild($thumbnail_node);

当然,将 setAttribute 中的文字值更改为变量或文字,以提供您想要呈现的实际数据。

以下是该片段实际生成的内容:

<media:thumbnail URL="http://www.foo.com/keyframe.jpg" width="75" height="75" time="12:05:01.123"/>