Skip to content

Instantly share code, notes, and snippets.

@GDmac
Last active April 5, 2019 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GDmac/5535136 to your computer and use it in GitHub Desktop.
Save GDmac/5535136 to your computer and use it in GitHub Desktop.
<?php
$xml = new SimpleXMLElement(
'<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
</channel>
</rss>');
$channel = $xml->channel;
// .. fetch items from database
foreach ($items as $item)
{
// image or clip
if (in_array($item->type, array('image','clip')))
{
$feeditem = $channel->addChild('item');
$feeditem->addChild('title', $item->title);
$feeditem->addChild('description', $item->title);
$feeditem->addChild('link', $item->get_fullpath());
$guid = $feeditem->addChild('guid', $item->get_guid());
$guid->addAttribute('isPermalink', 'false');
$media_content = $feeditem->addChild('media:content', '', 'http://search.yahoo.com/mrss/');
$media_content->addAttribute('url', $item->get_fullpath());
$media_content->addAttribute('medium', $item->get_feedtype());
if ($item->type=='image') $media_content->addAttribute('duration', $item->duration);
}
// external feed
if ($item->type == 'feed')
{
$feedxml = simplexml_load_file($item->filename);
if ($feedxml == false) die('cant read feed');
foreach($feedxml->channel->item as $feeditem)
{
xml_adopt($channel, $feeditem);
}
}
}
// debug pretty print
$dom = new DOMDocument("1.0");
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
echo "<pre>";
echo htmlspecialchars( var_export($dom->saveXML(), true) );
// ----------------------------------------------------------------------
function xml_adopt($root, $new, $namespace = null) {
// first add the new node
$node = $root->addChild($new->getName(), (string) $new, $namespace);
// add any attributes for the new node
foreach($new->attributes() as $attr => $value) {
$node->addAttribute($attr, $value);
}
// get all namespaces, include a blank one
$namespaces = array_merge(array(null), $new->getNameSpaces(true));
// add any child nodes, including optional namespace
foreach($namespaces as $space) {
foreach ($new->children($space) as $child) {
xml_adopt($node, $child, $space);
}
}
}
// ----------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<item>
<title>cheese.jpg</title>
<description>cheese.jpg</description>
<link>http://local.dev/zwartav-rss/feedmanager/media/cheese.jpg</link>
<guid isPermalink="false">cb3415a3e8660538c7dcc20d91b9fc28e8063085</guid>
<media:content url="http://local.dev/zwartav-rss/feedmanager/media/cheese.jpg" medium="image" duration="20"/>
<feed_id>1f506a1261ee0374caddede812524456</feed_id>
</item>
<item>
<title>cocacola.jpg</title>
<description>cocacola.jpg</description>
<link>http://local.dev/zwartav-rss/feedmanager/media/cocacola.jpg</link>
<guid isPermalink="false">cd7e0cef4f98c8cec7fb7c6618ba3526d113ad92</guid>
<media:content url="http://local.dev/zwartav-rss/feedmanager/media/cocacola.jpg" medium="image" duration="15"/>
<feed_id>1f506a1261ee0374caddede812524456</feed_id>
</item>
<item>
<title>video.mp4</title>
<description>video.mp4</description>
<link>http://local.dev/zwartav-rss/feedmanager/media/video.mp4</link>
<guid isPermalink="false">f1c095f4b1c923dbbf459da6eff04b727dd1f35c</guid>
<media:content url="http://local.dev/zwartav-rss/feedmanager/media/video.mp4" medium="video"/>
<feed_id>1f506a1261ee0374caddede812524456</feed_id>
</item>
</channel>
</rss>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment