簡體   English   中英

使用 PHP 和 Mysqli 創建 RSS 提要

[英]Create RSS feeds with PHP and Mysqli

我沒有類別的 rss 提要,所以我正在嘗試為新帖子和按類別發布的帖子創建“rss php”文件

更新

到目前為止,我在@jonasdev 的幫助下做到了這一點。

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


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

我沒有從數據庫中得到任何東西

我不知道我做錯了什么。

帖子表在此處輸入圖像描述 類別表在此處輸入圖像描述

我認為您的直接問題是您缺少CTYPE中的關閉>因此您會收到解析錯誤。 也不建議在文件末尾使用 PHP 結束標記?> ,尤其是在使用對額外空格相當敏感的 XML 格式時。 我還建議您將功能包裝在函數中,以便更容易閱讀和調試。 這是一個建議的實現(我已經用返回一個“行”的 function 替換了您的數據庫請求,因為我無權訪問您的數據庫)。

<?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
    ];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM