使用PHP和Mysqli创建RSS提要

问题描述 投票:0回答:1

我没有类别的rss feed,所以我试图为新帖子和按类别创建'rss php'文件

更新

到目前为止,我在@jonasdev的帮助下完成了此操作。

 <?php
define('PAGE', 'site');
// Create connection
 $con=mysqli_connect("akhbar1.com","user","pass");
// Check connection
 if (mysqli_connect_errno($con)) {
 echo "Database connection failed!: " . mysqli_connect_error();
 }

 $sql = "SELECT * FROM in_posts ORDER BY post_id DESC LIMIT $feed_limit";
 $query = mysqli_query($con,$sql);
function rss_date($time) {
    return gmdate('r', $time);
}
$feed_limit = (int) $_GET['l'];
if ($feed_limit <= 0 || $feed_limit >= 100) {
    $feed_limit = 500;
}

render($_GET['r']);

/**
 * Renders the page
 * @param $type
 */
function render($type) {

    $items = [];

    render_header();
    switch($type) {
        case 'newpost':
            $items[] = getItemFromDatabase(); 
            break;
        case 'category':
            if(isset($_GET['id'])){
               $get_idy = $_GET['id'];
                $items[] = getCategoryItemFromDatabase(); 
               } else {
               echo "failed";
            }


            break;
        default:
            $items[] = getItemFromDatabase(); 
    }

    foreach($items as $item) {
        render_item($item["post_id"], $item["post_title"], $item["post_excerp"], $item["created_at"]);
    }
    render_footer();
}

/**
 * Renders opening RSS
 */
function render_header() {
    header ('Content-type: text/xml');
    echo <<< RSS
<?xml version="1.0" encoding="UTF-8" ?>
    <rss version="2.0">
    <channel>
        <title>title</title>
        <link>https://www.akhbar1.com/</link>
        <description>description</description>
RSS;
}

/**
 * Renders closing RSS
 */
function render_footer() {
    echo <<< RSS
    </channel>
</rss>
RSS;
}

/**
 * Renders one RSS Item
 * 
 * @param $postId
 * @param $postTitle
 * @param $postExcerp
 * @param $createdAt
 */
function render_item($postId, $postTitle, $postExcerp, $createdAt) {
    $rssDate = rss_date($createdAt);
    echo <<< RSS
    <item>
        <title><![CDATA[$postTitle]]></title>
        <description>$postExcerp</description>
        <link>https://www.akhbar1.com/news-$postId.html</link>
        <pubDate>$rssDate</pubDate>
    </item>
RSS;

}

/**
 * 
 * @return array
 */
function getItemFromDatabase() {
 while($row = mysqli_fetch_array($con,$query)){
   $title=$row["post_title"];
   $link=$row["post_id"];
   $description=$row["post_content"];
   $createdAt=$row["created_at"];

    return [
        "post_id" => $link,
        "post_title" => $title,
        "post_exerp" => $description,
        "created_at" => $createdAt,
    ];
    }
}
function getCategoryItemFromDatabase() {
 $sel = mysqli_query($con,"SELECT * FROM in_posts WHERE post_category_id='$get_idy' ORDER BY post_id DESC LIMIT $feed_limit");
 while($row = mysqli_fetch_array($con,$sel)){
   $title=$row["post_title"];
   $link=$row["post_id"];
   $description=$row["post_content"];
   $createdAt=$row["created_at"];

    return [
        "post_id" => $link,
        "post_title" => $title,
        "post_exerp" => $description,
        "created_at" => $createdAt,
    ];
    }
}

提要新帖子https://www.akhbar1.com/rss.php?r=newpost&l=10

按类别供稿https://www.akhbar1.com/rss.php?r=category&id=1&l=10

我没有从数据库中得到任何东西

我不知道我在做什么错。

帖子表enter image description here类别表enter image description here

php rss
1个回答
1
投票

[我认为您的直接问题是,您在>中缺少结尾CTYPE,因此您将获得解析错误。还不建议在文件末尾使用PHP结束标记?>,尤其是在使用XML格式时,该格式对多余的空格非常敏感。我还建议您将功能包装在函数中,这样更易​​于阅读和调试。这是一个建议的实现(我已用返回一个“行”的函数替换了您的数据库请求,因为我无权访问您的数据库)。

<?php
define('PAGE', 'site');
function rss_date($time) {
    return gmdate('r', $time);
}
$feed_limit = (int) $_GET['l'];
if ($feed_limit <= 0 || $feed_limit >= 100) {
    $feed_limit = 500;
}

render($_GET['r']);

/**
 * Renders the page
 * @param $type
 */
function render($type) {

    $items = [];

    render_header();
    switch($type) {
        case 'newpost':
            $items[] = getItemFromDatabase(); // Replace with database request
            break;
        case 'category':
            $items[] = getItemFromDatabase(); // Replace with database request
            break;
        default:
            $items[] = getItemFromDatabase(); // Replace with database request
    }

    foreach($items as $item) {
        render_item($item["post_id"], $item["post_title"], $item["post_excerp"], $item["created_at"]);
    }
    render_footer();
}

/**
 * Renders opening RSS
 */
function render_header() {
    header ('Content-type: text/xml');
    echo <<< RSS
<?xml version="1.0" encoding="UTF-8" ?>
    <rss version="2.0">
    <channel>
        <title>title</title>
        <link>https://www.akhbar1.com/</link>
        <description>description</description>
RSS;
}

/**
 * Renders closing RSS
 */
function render_footer() {
    echo <<< RSS
    </channel>
</rss>
RSS;
}

/**
 * Renders one RSS Item
 * 
 * @param $postId
 * @param $postTitle
 * @param $postExcerp
 * @param $createdAt
 */
function render_item($postId, $postTitle, $postExcerp, $createdAt) {
    $url = null; // Declare $url to something!
    $rssDate = rss_date($createdAt);
    echo <<< RSS
    <item>
        <title><![CDATA[$postTitle]]></title>
        <description>$postExcerp</description>
        <link>https://www.akhbar1.com/{$url->file($postId, $postTitle)}</link>
        <pubDate>$rssDate</pubDate>
    </item>
RSS;

}

/**
 * Just for test purposes, remove and fetch from DB
 * @return array
 */
function getItemFromDatabase() {
    return [
        "post_id" => 1,
        "post_title" => "my item",
        "post_exerp" => "my description",
        "created_at" => 1221412212
    ];
}
© www.soinside.com 2019 - 2024. All rights reserved.