質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
RSS

RSS(Really Simple Syndication)はブログのエントリやニュースの見出し、標準のフォーマットの音声やビデオなどを発行するために使われるウェブフィードのフォーマットの集合体です。

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

1回答

1085閲覧

WordPressに外部ブログの記事を表示したい

退会済みユーザー

退会済みユーザー

総合スコア0

RSS

RSS(Really Simple Syndication)はブログのエントリやニュースの見出し、標準のフォーマットの音声やビデオなどを発行するために使われるウェブフィードのフォーマットの集合体です。

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2021/04/09 04:26

編集2021/04/09 06:54

前提・実現したいこと

WordPressサイトのトップページに、外部ブログ(ライブドアブログ)の最新記事を一件だけ表示させたいのですが、RSSが取得されず、何も表示されません。
よろしければご助言いただけますと幸いです。

参考サイト:https://innervisions.info/wordpress/rss-read/

PHP

1<?php 2include_once( ABSPATH . WPINC . '/feed.php' ); 3$rss = fetch_feed( 'http://hogehoge.doorblog.jp/index.rdf' ); // ここにURLを入力する 4if ( !is_wp_error( $rss ) ) { 5$maxitems = $rss->get_item_quantity( 1 ); 6$rss_items = $rss->get_items( 0, $maxitems ); 7} 8?> 9<?php if ( !empty( $maxitems ) ) : ?> 10<?php if ($maxitems == 0) echo 'RSSデータがありませんでした.'; 11else 12foreach ( $rss_items as $item ) : ?> 13 14<a href="<?php echo $item->get_permalink(); ?>"> 15<?php echo $item->get_date('Y.m.d');// 日付 ?><span><?php echo $item->get_title();// タイトル ?></span> 16</a> 17 18<?php endforeach; ?> 19<?php endif; ?>

Feed.phpのコード内容です。

Feed.php

1<?php 2 3/** 4 * RSS for PHP - small and easy-to-use library for consuming an RSS Feed 5 * 6 * @copyright Copyright (c) 2008 David Grudl 7 * @license New BSD License 8 * @version 1.5 9 */ 10class Feed 11{ 12 /** @var int */ 13 public static $cacheExpire = '1 day'; 14 15 /** @var string */ 16 public static $cacheDir; 17 18 /** @var string */ 19 public static $userAgent = 'FeedFetcher-Google'; 20 21 /** @var SimpleXMLElement */ 22 protected $xml; 23 24 25 /** 26 * Loads RSS or Atom feed. 27 * @param string 28 * @param string 29 * @param string 30 * @return Feed 31 * @throws FeedException 32 */ 33 public static function load($url, $user = null, $pass = null) 34 { 35 $xml = self::loadXml($url, $user, $pass); 36 if ($xml->channel) { 37 return self::fromRss($xml); 38 } else { 39 return self::fromAtom($xml); 40 } 41 } 42 43 44 /** 45 * Loads RSS feed. 46 * @param string RSS feed URL 47 * @param string optional user name 48 * @param string optional password 49 * @return Feed 50 * @throws FeedException 51 */ 52 public static function loadRss($url, $user = null, $pass = null) 53 { 54 return self::fromRss(self::loadXml($url, $user, $pass)); 55 } 56 57 58 /** 59 * Loads Atom feed. 60 * @param string Atom feed URL 61 * @param string optional user name 62 * @param string optional password 63 * @return Feed 64 * @throws FeedException 65 */ 66 public static function loadAtom($url, $user = null, $pass = null) 67 { 68 return self::fromAtom(self::loadXml($url, $user, $pass)); 69 } 70 71 72 private static function fromRss(SimpleXMLElement $xml) 73 { 74 if (!$xml->channel) { 75 throw new FeedException('Invalid feed.'); 76 } 77 78 self::adjustNamespaces($xml); 79 80 foreach ($xml->channel->item as $item) { 81 // converts namespaces to dotted tags 82 self::adjustNamespaces($item); 83 84 // generate 'url' & 'timestamp' tags 85 $item->url = (string) $item->link; 86 if (isset($item->{'dc:date'})) { 87 $item->timestamp = strtotime($item->{'dc:date'}); 88 } elseif (isset($item->pubDate)) { 89 $item->timestamp = strtotime($item->pubDate); 90 } 91 } 92 $feed = new self; 93 $feed->xml = $xml->channel; 94 return $feed; 95 } 96 97 98 private static function fromAtom(SimpleXMLElement $xml) 99 { 100 if (!in_array('http://www.w3.org/2005/Atom', $xml->getDocNamespaces(), true) 101 && !in_array('http://purl.org/atom/ns#', $xml->getDocNamespaces(), true) 102 ) { 103 throw new FeedException('Invalid feed.'); 104 } 105 106 // generate 'url' & 'timestamp' tags 107 foreach ($xml->entry as $entry) { 108 $entry->url = (string) $entry->link['href']; 109 $entry->timestamp = strtotime($entry->updated); 110 } 111 $feed = new self; 112 $feed->xml = $xml; 113 return $feed; 114 } 115 116 117 /** 118 * Returns property value. Do not call directly. 119 * @param string tag name 120 * @return SimpleXMLElement 121 */ 122 public function __get($name) 123 { 124 return $this->xml->{$name}; 125 } 126 127 128 /** 129 * Sets value of a property. Do not call directly. 130 * @param string property name 131 * @param mixed property value 132 * @return void 133 */ 134 public function __set($name, $value) 135 { 136 throw new Exception("Cannot assign to a read-only property '$name'."); 137 } 138 139 140 /** 141 * Converts a SimpleXMLElement into an array. 142 * @param SimpleXMLElement 143 * @return array 144 */ 145 public function toArray(SimpleXMLElement $xml = null) 146 { 147 if ($xml === null) { 148 $xml = $this->xml; 149 } 150 151 if (!$xml->children()) { 152 return (string) $xml; 153 } 154 155 $arr = []; 156 foreach ($xml->children() as $tag => $child) { 157 if (count($xml->$tag) === 1) { 158 $arr[$tag] = $this->toArray($child); 159 } else { 160 $arr[$tag][] = $this->toArray($child); 161 } 162 } 163 164 return $arr; 165 } 166 167 168 /** 169 * Load XML from cache or HTTP. 170 * @param string 171 * @param string 172 * @param string 173 * @return SimpleXMLElement 174 * @throws FeedException 175 */ 176 private static function loadXml($url, $user, $pass) 177 { 178 $e = self::$cacheExpire; 179 $cacheFile = self::$cacheDir . '/feed.' . md5(serialize(func_get_args())) . '.xml'; 180 181 if (self::$cacheDir 182 && (time() - @filemtime($cacheFile) <= (is_string($e) ? strtotime($e) - time() : $e)) 183 && $data = @file_get_contents($cacheFile) 184 ) { 185 // ok 186 } elseif ($data = trim(self::httpRequest($url, $user, $pass))) { 187 if (self::$cacheDir) { 188 file_put_contents($cacheFile, $data); 189 } 190 } elseif (self::$cacheDir && $data = @file_get_contents($cacheFile)) { 191 // ok 192 } else { 193 throw new FeedException('Cannot load feed.'); 194 } 195 196 return new SimpleXMLElement($data, LIBXML_NOWARNING | LIBXML_NOERROR | LIBXML_NOCDATA); 197 } 198 199 200 /** 201 * Process HTTP request. 202 * @param string 203 * @param string 204 * @param string 205 * @return string|false 206 * @throws FeedException 207 */ 208 private static function httpRequest($url, $user, $pass) 209 { 210 if (extension_loaded('curl')) { 211 $curl = curl_init(); 212 curl_setopt($curl, CURLOPT_URL, $url); 213 if ($user !== null || $pass !== null) { 214 curl_setopt($curl, CURLOPT_USERPWD, "$user:$pass"); 215 } 216 curl_setopt($curl, CURLOPT_USERAGENT, self::$userAgent); // some feeds require a user agent 217 curl_setopt($curl, CURLOPT_HEADER, false); 218 curl_setopt($curl, CURLOPT_TIMEOUT, 20); 219 curl_setopt($curl, CURLOPT_ENCODING, ''); 220 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // no echo, just return result 221 if (!ini_get('open_basedir')) { 222 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // sometime is useful :) 223 } 224 $result = curl_exec($curl); 225 return curl_errno($curl) === 0 && curl_getinfo($curl, CURLINFO_HTTP_CODE) === 200 226 ? $result 227 : false; 228 229 } else { 230 $context = null; 231 if ($user !== null && $pass !== null) { 232 $options = [ 233 'http' => [ 234 'method' => 'GET', 235 'header' => 'Authorization: Basic ' . base64_encode($user . ':' . $pass) . "\r\n", 236 ], 237 ]; 238 $context = stream_context_create($options); 239 } 240 241 return file_get_contents($url, false, $context); 242 } 243 } 244 245 246 /** 247 * Generates better accessible namespaced tags. 248 * @param SimpleXMLElement 249 * @return void 250 */ 251 private static function adjustNamespaces($el) 252 { 253 foreach ($el->getNamespaces(true) as $prefix => $ns) { 254 $children = $el->children($ns); 255 foreach ($children as $tag => $content) { 256 $el->{$prefix . ':' . $tag} = $content; 257 } 258 } 259 } 260} 261 262 263 264/** 265 * An exception generated by Feed. 266 */ 267class FeedException extends Exception 268{ 269} 270

補足情報(FW/ツールのバージョンなど)

利用しているサーバーの都合で、PHPのバージョンが7.1で止まっています。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

miyabi_takatsuk

2021/04/09 06:11

ABSPATH . WPINC . '/feed.php' のファイルのソースコード内容も記載してください。 その処理内容もみないと何とも言えません。 質問は編集できます。 また、ソースコードは、コードブロックを使ってください。 ```言語名 ここにソースコード ``` という形式で入れると、コードブロックになります。
退会済みユーザー

退会済みユーザー

2021/04/09 06:59

コメントありがとうございます。ご指摘いただきましたfeed.phpの内容を記載いたしました。 また、コードをコードブロックで記載いたしました。 何卒よろしくお願いいたします。
CHERRY

2021/04/09 08:24 編集

楽天のRSSでは、どのようなデータが取得されるのでしょうか? RSS として、正しいフォーマットになっていますか?
退会済みユーザー

退会済みユーザー

2021/04/09 09:55

コメントありがとうございます。 こちらの勉強不足で申し訳ありません。「楽天のRSSでは」とはどういった意味でしょうか?
CHERRY

2021/04/09 10:37 編集

> 外部ブログ(ライブドアブログ)の最新記事を一件だけ表示させたいのですが、RSSが取得されず、何も表示されません。 > $rss = fetch_feed( 'http://hogehoge.doorblog.jp/index.rdf' ); // ここにURLを入力する この時点で、$rss に、データが入っていないということであれば、http://hogehoge.doorblog.jp/index.rdf が、正しい RSS のデータを返しているのかが気になりました。
退会済みユーザー

退会済みユーザー

2021/04/13 01:42

>CHERRY様 お返事いただきありがとうございました。確認大変遅くなり申し訳ありません。 楽天のRSSと、試しに別サービスのブログのRSS(アメブロ、FC2ブログなど)を入れてみたところ、希望の取得ができておりました。 ライブドアブログのRSSデータのみ、取得できないといったようでした。
guest

回答1

0

ベストアンサー

どうやらサーバーが原因だったようです。
別のテストサーバーを設定したら表示されました。お手数おかけしました。

投稿2021/04/16 07:04

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問