Yêu cầu thg 9 14, 2020 2:23 CH 457 0 1
  • 457 0 1
0

Xin bài hướng dẫn về laravel dùng RSS lấy dữ liệu vào web

Chia sẻ
  • 457 0 1

lục tìm mãi không thấy có bài nào hướng dẫn về việc lấy data tin tức từ 1 trang web khác về web của mình thông qua RSS, có pro nào có bài hướng dẫn vui lòng chỉ giúp với. Thanks

1 CÂU TRẢ LỜI


Đã trả lời thg 9 15, 2020 2:53 SA
Đã được chấp nhận
+2

Cách đơn giản nhất là bạn dùng thư viện https://github.com/willvincent/feeds này. để đọc rss từ các trang khác. Cài đặt:

composer require willvincent/feeds

Chạy câu lệnh để publish file config:

php artisan vendor:publish --provider="willvincent\Feeds\FeedsServiceProvider"

Sử dụng : controller

use willvincent\Feeds\Facades\FeedsFacade;

public function demo() {
  $feed = FeedsFacade::make('https://viblo.asia/rss/posts/newest.rss');
 $data = array(
      'title'     => $feed->get_title(),
      'permalink' => $feed->get_permalink(),
      'items'     => $feed->get_items(),
    );

    return View::make('feed', $data);
}

View:

extends('app')

@section('content')
  <div class="header">
    <h1><a href="{{ $permalink }}">{{ $title }}</a></h1>
  </div>

  @foreach ($items as $item)
    <div class="item">
      <h2><a href="{{ $item->get_permalink() }}">{{ $item->get_title() }}</a></h2>
      <p>{{ $item->get_description() }}</p>
      <p><small>Posted on {{ $item->get_pubDate('j F Y | g:i a') }}</small></p>
    </div>
  @endforeach
@endsection

Bạn có thể đọc doc của thư viện ở đây : https://github.com/willvincent/feeds

Chia sẻ
Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí