LoginSignup
2
6

More than 5 years have passed since last update.

アメブロやはてなブログなどの記事を埋め込む (PHP, RSS, Atom)

Last updated at Posted at 2018-08-07

RSS & Atom Feeds for PHP - github で簡単に埋め込める
キャッシュ機能もある

ディレクトリ構造

├ index.php (埋め込み先のページ)
├ Feed.php (ダウンロードしたもの)
├ cache (キャッシュ用ディレクトリ)

サンプルコード

PHP

<?php
require_once './Feed.php' ; // rss-phpライブラリを読み込みます

$url       = 'http://sample.com/rss'; // RSSのURL
$num       = 12; // 表示させたい件数
$desc_max  = 60; // 詳細の最大文字数。0で制限なし
$desc_more = '…'; // 最大文字数を超えたら表示するテキスト
$desc_del  = '▼続きを読む'; // 詳細から削除したい文字列があれば (「続きを読む」など)
$alt_img   = 'http://dummyimage.com/600x400/CCC/fff.png&text=代替テキスト'; // 記事に画像がない場合に表示する画像

// キャッシュ (有効化時は cache ディレクトリ要作成。不要なら下記2行を削除)
Feed::$cacheDir    = __DIR__ . '/cache';
Feed::$cacheExpire = '2 hours';


if ( $desc_max != 0 ){ $desc_max = ( $desc_max * 2 ) + 4; } // 日本語文字数計算 (ブログによりずれるかも)


$rss = Feed::loadRss( $url ); // RSS。Atomなら Feed::loadAtom();


echo '<p>タイトル:<a href="' . $rss->link . '" target="_blank">' . $rss->title . '</a></p>';


/* ★★ ここに記事を取得できれば処理という条件分岐を入れたい */


echo "<ul class='feed-articles'>\n";

$i = 0;

foreach( $rss->item as $item ) {

    if( $i < $num ){

        $title       = $item->title ; // タイトル
        $link        = $item->link ; // リンク
        $timestamp   = strtotime( $item->pubDate ); // 更新日時
        $description = $item->description ; // 詳細
        // $html_encoded_content = $item->{'content:encoded'};

        $description = str_replace( $desc_del, '', $description );
        $description = strip_tags( $description );
        if ( $desc_max != 0 ){
            $description = mb_strimwidth( $description, 0, $desc_max, $desc_more, 'utf-8' );
        }

        $thumbnail = $alt_img; // ★★画像があってもメモリに代替え画像をいちいち読み込むのは無駄なので要改善

        if( preg_match_all( '/<img([\s\S]+?)>/is', $item->description, $imgs_in_desc ) ){
            foreach( $imgs_in_desc[0] as $img ){
                if ( $img === reset( $imgs_in_desc[0] ) ) {// 最初の画像にマッチしたもの
                    if( preg_match('/src=[\'"](.+?jpe?g)[\'"]/', $img, $m ) ){
                        $thumbnail = $m[1];
                    }
                }
            }
        }
        ?>
        <li class="feed-articles__item">
            <a href="<?php echo $link; ?>" target="_blank"><img class="feed-articles__item__img" src="<?php print $thumbnail; ?>" alt="<?php print $item->title; ?>" width="100"></a>
            <div class="feed-articles__item__body">
                <p class="feed-articles__item__title"><a href="<?php echo $link; ?>" target="_blank"><?php echo $title; ?></a></p>
                <p class="feed-articles__item__description"><?php echo $description; ?></p>
                <p class="feed-articles__item__date"><?php echo date( 'Y/m/d', $timestamp ); ?></p>
                <!-- <pclass="feed-articles__item__content"><?php echo $html_encoded_content; ?></p> -->
            </div>
        </li>
    <?php
        $i++;
    }
}

echo "</ul>\n";
?>

参考CSS

    img:hover {
        opacity: .8;
    }
    .feed-articles { /* ul */
        list-style-type: none;
        padding-left: 0;
        border: 1px solid silver;
    }
        .feed-articles p {
            margin-top: 0;
            margin-bottom: 0;
        }
        .feed-articles__item { /* li */
            display: flex;
            margin-bottom: 1.5rem;
        }
            .feed-articles__item__img { /* img */
                margin-right: 1rem;
            }
            .feed-articles__item__body { /* div */
            }
                .feed-articles__item__title { /* p */
                    font-weight: bold;
                }
                .feed-articles__item__description { /* p */
                }
                .feed-articles__item__date { /* p */
                    font-size: small;
                }

参考記事

2
6
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
2
6