12
8

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 5 years have passed since last update.

乃木坂で学ぶLaravelでRSSをパースしてAPIを作成する方法

Last updated at Posted at 2018-12-25

概要

まとめサイトからRSSを使ってAPIを作成したいと思いました。

対象のサイトは

乃木坂46まとめブログ
RSS: http://ngzk46.livedoor.blog/index.rdf

こちらのサイトのRSSを使います。ちなみにどのサイトのRSSでも問題ありません。
よくあるXMLをパースしてJSONを吐き出すところまでを実装していきます。

プロジェクトファイルのPathに移動する

$ cd (プロジェクトのディレクトリ)

Laravelプロジェクトファイルを作成

ディレクトリでLaravelをインストールする(今回はLaravel 5.5系で行う)

$ composer create-project --prefer-dist laravel/laravel PracticeLaravel "5.5.*"
$ cd PracticeLaravel

Controllerを作成する

$ php artisan make:controller ApiController

# Controller created successfully.

ルーティングの設定を行う

routes/web.php
// 初期設定はとりあえず削除する
//Route::get('/', function () {
//    return view('welcome');
//});

// ルートを設定する
Route::get('/', 'ApiController@index');

ControllerでRSSのXMLを読み込んで連想配列に変換してJSONにして吐き出す

参考サイトはこちら

ApiController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ApiController extends Controller
{
    public function index()
    {
        $xml = file_get_contents('http://ngzk46.livedoor.blog/index.rdf');
        $xmlObject = simplexml_load_string($xml);
        $xmlArray = json_decode( json_encode( $xmlObject ), TRUE );
        return $xmlArray;
    }
}

ローカルサーバーを立ち上げる

$ php artisan serve

# Laravel development server started: <http://127.0.0.1:8000>

http://127.0.0.1:8000 にアクセスする

Jsonとして出力されることが確認できます。

スクリーンショット 2018-12-23 17.15.04.png

ただ、これだとどのパラメータをパースすればいいのかわからないので大抵はクロムの拡張機能を使って見やすいように修飾します。

JSONView

JSONViewをクロムに追加すると下記のようにJSONが綺麗になります。

スクリーンショット 2018-12-23 17.10.40.png

実際にパラメータをパースする

  • channel
ApiController.php

class ApiController extends Controller
{
    public function index()
    {
        $xml = file_get_contents('http://ngzk46.livedoor.blog/index.rdf');
        $xmlObject = simplexml_load_string($xml);
        $xmlArray = json_decode( json_encode( $xmlObject ), TRUE );
        return $xmlArray["channel"];
    }
}

結果は

スクリーンショット 2018-12-23 17.20.29.png
  • item
ApiController.php
class ApiController extends Controller
{
    public function index()
    {
        $xml = file_get_contents('http://ngzk46.livedoor.blog/index.rdf');
        $xmlObject = simplexml_load_string($xml);
        $xmlArray = json_decode( json_encode( $xmlObject ), TRUE );
        return $xmlArray['item'];
    }
}
スクリーンショット 2018-12-23 17.23.56.png
  • 一つ目のitem
ApiController.php
class ApiController extends Controller
{
    public function index()
    {
        $xml = file_get_contents('http://ngzk46.livedoor.blog/index.rdf');
        $xmlObject = simplexml_load_string($xml);
        $xmlArray = json_decode( json_encode( $xmlObject ), TRUE );
        return $xmlArray['item'][0];
    }
}
スクリーンショット 2018-12-23 17.22.32.png

こんな感じの使い方になります。
これでモバイルアプリの人も簡単にAPIを作成できますね。

12
8
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
12
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?