6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Web × PHP TechCafeAdvent Calendar 2019

Day 24

[php]シンプルなRSSのサンプル

Last updated at Posted at 2019-12-25

RSSのサンプルを実装してみましたので
自分用メモとして残します。
特に説明は載せませんので、詳しく知りたい方は
下記参考サイトを参照してください。

参考
【PHP入門】完全マスター!XMLを自由自在に扱う方法まとめ

RSS配信している人気サイト一覧

コード全文

<?php
    $val_datas = array();
    $xml = simplexml_load_file('http://www.soumu.go.jp/news.rdf');

    foreach($xml as $val) {
        $x = array();
        $x['title'] = (string)$val->title;
        $x['link'] = (string)$val->link;
        $val_datas[] = $x;
    }
    // echo '<pre>';print_r($val_datas);echo '</pre>';
    // echo '<pre>';print_r($xml);echo '</pre>';
?>

<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta charset="utf-8">
        <title>XML | RSS</title>
    </head>
    <body>
        <div class="contents">
            <?php foreach($val_datas as $val_data): ?>
                <div class="rss-set">
                    <div class="rss-title"><?= $val_data['title']; ?></div>
                    <div class="rss-link"><a href="<?= $val_data['link']; ?>" target="_blank"><?= $val_data['link']; ?></a></div>
                </div>
            <?php endforeach ?>
        </div>
    </body>
</html>

<style>
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
a {
    color: #4169e1;
    text-decoration: none;
}
body {
    background: #fcfcfc;
}
.contents {
    width: 75%;
    margin: 50px auto;
}
.contents .rss-set {
    font-size: 14px;
    margin: 0 0 15px 0;
    padding: 20px;
    background: #eee;
}
.contents .rss-set .rss-link {
    overflow-wrap: break-word;
}
</style>

[CDATA] SimpleXMLElementの使い方

参考

<?php
    $rss_url = "http://tor-acofes.com/blog/feed/";
    $rss_data = simplexml_load_file( $rss_url, 'SimpleXMLElement', LIBXML_NOCDATA );
?>

<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta charset="utf-8">
        <title>XML | RSS</title>
    </head>
    <body>
        <div class="contents">
            <?php foreach ( $rss_data->channel->item as $item ): ?>
                <div class="rss-set">
                    <div class="rss-title"><?= $item->title; ?></div>
                    <div class="rss-description">
                        <?= mb_substr($item->description, 1, 100); ?>
                        <a href="<?= $item->link; ?>" target="_blank">… 続きを読む →</a>
                    </div>
                    <div class="rss-date">
                        <?php
                            $datetime = date("Y.n.j", strtotime($item->pubDate));
                            echo $datetime;
                        ?>
                    </div>
                </div>
            <?php endforeach ?>
        </div>
    </body>
</html>

<style>
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
a {
    color: #4169e1;
    text-decoration: none;
}
body {
    background: #fcfcfc;
}
.contents {
    width: 75%;
    margin: 50px auto;
}
.contents .rss-set {
    font-size: 14px;
    margin: 0 0 15px 0;
    padding: 20px;
    background: #eee;
}
.contents .rss-set .rss-link {
    overflow-wrap: break-word;
}
</style>
6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?