从 PHP 中的动态内容创建简单的 RSS
Creating a simple RSS from dynamic content in PHP
我正在 PHP 中创建一个包含动态内容的简单 RSS,我正在使用以下代码:
PHP代码:
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$root = $doc->createElement('rss');
$root = $doc->appendChild($root);
$xml = simplexml_load_string($xml_data->asXML());
foreach($xml->data->item as $item)
{
$title = $doc->createElement('title');
$title = $root->appendChild($title);
$text = $doc->createTextNode($item->title);
$text = $title->appendChild($text);
$link = $doc->createElement('link');
$link = $root->appendChild($link);
$text = $doc->createTextNode("http://example.com/xyz/?zyx=".$item->id);
$text = $link->appendChild($text);
}
echo 'Wrote: ' . $doc->save("/directory/jobs00.xml") . ' bytes';
我用上面的代码得到了什么结果:
<rss>
<title>title1</title>
<link>http://example.com/xyz/?zyx=11008</link>
<title>title2</title>
<link>http:/example.com/xyz/?zyx=11009</link>
</rss>
我想要什么结果:
<rss>
<channel>
<item>
<title>title1</title>
<link>http://example.com/xyz/?zyx=11008</link>
</item>
<item>
<title>title2</title>
<link>http://example.com/xyz/?zyx=11009</link>
</item>
</channel>
</rss>
所以我需要在我的代码中修改什么才能实现我上面想要的。
您可能需要调整其构建方式,但它只是您当前所拥有内容的扩展...
$xml = simplexml_load_string($xml_data->asXML());
$channel = $doc->createElement('channel');
$root->appendChild($channel);
foreach($xml->data->item as $item)
{
$title = $doc->createElement('title');
$text = $doc->createTextNode($item->title);
$text = $title->appendChild($text);
$link = $doc->createElement('link');
$text = $doc->createTextNode("http://example.com/xyz/?zyx=".$item->id);
$text = $link->appendChild($text);
$item = $doc->createElement('item');
$item->appendChild($title);
$item->appendChild($link);
$channel->appendChild($item);
}
我正在 PHP 中创建一个包含动态内容的简单 RSS,我正在使用以下代码:
PHP代码:
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$root = $doc->createElement('rss');
$root = $doc->appendChild($root);
$xml = simplexml_load_string($xml_data->asXML());
foreach($xml->data->item as $item)
{
$title = $doc->createElement('title');
$title = $root->appendChild($title);
$text = $doc->createTextNode($item->title);
$text = $title->appendChild($text);
$link = $doc->createElement('link');
$link = $root->appendChild($link);
$text = $doc->createTextNode("http://example.com/xyz/?zyx=".$item->id);
$text = $link->appendChild($text);
}
echo 'Wrote: ' . $doc->save("/directory/jobs00.xml") . ' bytes';
我用上面的代码得到了什么结果:
<rss>
<title>title1</title>
<link>http://example.com/xyz/?zyx=11008</link>
<title>title2</title>
<link>http:/example.com/xyz/?zyx=11009</link>
</rss>
我想要什么结果:
<rss>
<channel>
<item>
<title>title1</title>
<link>http://example.com/xyz/?zyx=11008</link>
</item>
<item>
<title>title2</title>
<link>http://example.com/xyz/?zyx=11009</link>
</item>
</channel>
</rss>
所以我需要在我的代码中修改什么才能实现我上面想要的。
您可能需要调整其构建方式,但它只是您当前所拥有内容的扩展...
$xml = simplexml_load_string($xml_data->asXML());
$channel = $doc->createElement('channel');
$root->appendChild($channel);
foreach($xml->data->item as $item)
{
$title = $doc->createElement('title');
$text = $doc->createTextNode($item->title);
$text = $title->appendChild($text);
$link = $doc->createElement('link');
$text = $doc->createTextNode("http://example.com/xyz/?zyx=".$item->id);
$text = $link->appendChild($text);
$item = $doc->createElement('item');
$item->appendChild($title);
$item->appendChild($link);
$channel->appendChild($item);
}