LoginSignup
1
0

RSSを取得する方法

Last updated at Posted at 2024-03-06

はじめに

本記事は、JavaScript・HTMLでRSSの取得・表示を試みたときの簡単な備忘録です

RSSを取得する方法

結論

Javascriptで他所のサーバーのRSSを取得しようとするとCORSエラーが発生するためうまくいきませんでした
サーバーサイドで処理をすればCORSエラーを回避できるので、PHPを用いることによってRSSを取得することができました

ソースコード例

任意のRSSを取得・表示する最低限のソースコードを以下に記述します

php
<?php
function rss_get($url){
    $curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, $url);
	$rss = curl_exec($curl);
	curl_close($curl);
 
    echo $rss;
}
?>

<!DOCTYPE html>
<html lang="ja">
<head></head>
<body>
    <main>
        <div>
            <?php rss_get("任意のURL"); ?>
        </div>
    </main>
</body>
</html>
備考
  • php 7.4
  • 取得したデータの処理は別途必要です
  • simplexml_load_file()でもRSSを取得できますが、simplexmlが入っていない場合があったので今回は上記を採用しました
参考

https://www.php.net/manual/ja/function.curl-setopt.php
https://www.php.net/manual/ja/simplexml.examples-basic.php

1
0
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
1
0