设置RSS源(用于使用iTunes进行播客),从mysql数据库中读取信息(使用php)

时间:2012-02-06 18:42:22

标签: php rss itunes

我有一个RSS源,我有iTunes链接到用于播客(所有这些工作)。但是,我正在尝试设置它,以便我不必将每个播客数据硬编码到RSS源中。所以我这样做的方法是使用播客上传表单,将所有数据加载到mysql数据库中,然后在php中使用“for”循环将其读入feed中。将表单中的数据导入数据库非常有效,并且从数据库连接和读取也可以正常工作。但是,当我尝试将其实现到RSS提要时,没有“文章”显示出来。这是.rss文件的代码:

<?xml version="1.0" encoding="UTF-8" ?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
<channel>
<title>Victory Central Church Message Podcast</title>
<link>http://www.vc2online.com</link>
<language>en-us</language>
<copyright>&#x2117; &amp; &#xA9; 2012 Waller Hill Publishing</copyright>
<itunes:subtitle>Victory Central Church Podcast</itunes:subtitle>
<itunes:author>Victory Central Church</itunes:author>
<itunes:summary>The Sermons from Victory Central Church - A regional church sharing the life-giving message of Jesus Christ with Central Georgia and the world!</itunes:summary>
<description>The Sermons from Victory Central Church - A regional church sharing the life-giving message of Jesus Christ with Central Georgia and the world!</description>
<itunes:owner>
<itunes:name>Victory Central Church</itunes:name>
</itunes:owner>
<itunes:image href="images/podcasts_logo.jpg" />
<itunes:category text="Christianity">
<itunes:category text="Spirituality"/>
</itunes:category>

<?php

    $host = 'xxx.xx.xxx.xxx';
    $username = 'xxxx';
    $password = 'xxxx';
    $database = 'xxxx';

    mysql_connect($host, $username, $password) or die( "Unable to connect.");
    @mysql_select_db($database) or die( "Unable to select database.");

    $result = mysql_query("SELECT * FROM  `podcasts` WHERE 1 LIMIT 0 , 30");

    while ($row = mysql_fetch_array($result))
    {
        echo "<item>";
        echo "<title>" . $row['title'] . "</title>";
        echo "<itunes:author>" . $row['author'] . "</itunes:author>";
        echo "<itunes:subtitle>" . $row['subtitle'] . "</itunes:subtitle>";
        echo "<itunes:summary>" . $row['summary'] . "</itunes:summary>";
        echo "<itunes:image href=\"" . $row['imageurl'] . "\" />";
        echo "<enclosure url=\"" . $row['url'] . "\" type=\"" . $row['type'] . "\" />";
        echo "<guid>" . $row['guid'] . "</guid>";
        echo "<pubDate>" . $row['date'] . "</pubDate>";
        echo "<itunes:duration>" . $row['duration'] . "</itunes:duration>";
        echo "<itunes:keywords>" . $row['keywords'] . "</itunes:keywords>";
        echo "</item><br><p />";

    }

?>

</channel>
</rss>

当然主机,用户,密码和数据库都包含在实际代码中。当我按原样运行此代码时,不会显示任何错误。只有没有“文章”出现。任何帮助将非常感激。谢谢!

1 个答案:

答案 0 :(得分:0)

我为此创建了一个小型开源库。

您可以下载并了解如何使用它here,但让我举一个例子:

use iTunesPodcastFeed\Channel;
use iTunesPodcastFeed\FeedGenerator;
use iTunesPodcastFeed\Item;

require __DIR__ . '/vendor/autoload.php';

// SETUP CHANNEL
$title = 'Read2Me Daily Curated Articles';
$link = 'https://read2me.online';
$author = 'NYTimes and Medium';
$email = 'hello@read2me.online';
$image = 'https://d22fip447qchhd.cloudfront.net/api/widget/static/images/default-thumbnail.png';
$explicit = false;
$categories = [
    'News',
    'Technology',
    'Culture',
    'Entrepreneurship',
    'Productivity'
];
$description = 'Daily curated articles from New York Times and Medium';
$lang = 'en';
$copyright = 'The New York Times Company and The Medium Company';
$ttl = 43200; // 12 hours in seconds

$channel = new Channel(
    $title, $link, $author, $email,
    $image, $explicit, $categories,
    $description, $lang, $copyright, $ttl
);

// SETUP EPISODE
$title = "Trump Says Disclosure of Mueller Questions in Russia Probe Is ‘Disgraceful’";
$fileUrl = 'https://s3.read2me.online/audio/www-nytimes-com-2018-05-01-us-politics-trump-mueller-russia-questions-html-7e9601.mp3';
$duration = '2:18';
$description = 'WASHINGTON — President Trump on Tuesday said it was “disgraceful” that questions the special counsel would like to ask him were publicly disclosed, and he incorrectly noted that there were no questions about collusion. The president also said collusion was a “phony” crime.';
$date = 1525177808;
$filesize = 828387;
$mime = 'audio/mpeg';

$item = new Item(
    $title, $fileUrl, $duration,
    $description, $date, $filesize, $mime
);
$item2 = clone $item; // just to give you an idea of how it works

// SETUP FEED
$feed = new FeedGenerator($channel, ...[$item, $item2]);

// OUTPUT XML
header('Content-Type: application/xml; charset=utf-8');

print $feed->getXml();