laravelrssrss-readerfeedparsersimplepie

Cannot get thumbnail in rss feed in laravel


I am using willvincent feed reader to parse rss feeds, But i cannot seem to get the thumbail of the images, Here is my code

Route::get('feed', function(Request $request) {
    $f = FeedsFacade::make('http://www.cbn.com/cbnnews/us/feed/');

    // $results = [
    //     'image' => $f->get_image_url(),
    // ];
    
    foreach($f->get_items(0, $f->get_item_quantity()) as $item) {
        $i['title'] = $item->get_title();
        $i['thumbnail'] = $item->get_thumbnail();
        $i['description'] = $item->get_description();
        $i['content'] = $item->get_content();
        $i['link'] = $item->get_link();
        $i['date'] = $item->get_date();

        $results['items'][] = $i;
    }

    dd($results);

})->name('feed');

Thumbnail always return null, will appreciate anyone's help


Solution

  • Here is how we call image and other contents from rss feed

    NOTE: To get image, it is important that the site you fetching rss feeds, use to provide image too in their feed. Else won't get image in your fetched feed (there might be some trick to still fetch image but I am not aware of it for now). For example, google news feed don't provide image so you won't get image from google news feed.

    Here is example of one site who use to provide image.

    //In your CONTROLLER file
    
     $feed = Feeds::make('https://globalnews.ca/feed/');
        $data = array(
          'title'     => $feed->get_title(),
          'permalink' => $feed->get_permalink(),
          'items'     => $feed->get_items(),
        );
      
     return view('view_file', $data)
    
    //OR in case you are calling array value to show on view then it would be like this
    
    return view('view_file', array(
       'name' =>  $var 
     ), $data)   //notice "$data" at end of array.
    

    Now after done on controller part, this is how you will call in view file

    @foreach ($items as $item)
    
        //GET IMAGE
        @if($enclosure = $item->get_enclosure())
            <img src="{{$enclosure->get_thumbnail()}}">
        @endif
    
        //GET TITLE
        <div class="news-title">{{ $item->get_title() }}</div>
    
        //GET DESCRIPTION
        {{$item->get_description()}}
    
        //NOTE: this will bring html. TO show as output instead of html you can either use {!!  !!) or "strip_tags" function. And "substr" function to show first 100 characters only as small description
        {{ substr(strip_tags($item->get_description()), 0, 100) }}
    
       //GET LINK/URL OF NEWS
       <a href="{{ $item->get_permalink() }}" target="_blank">Read More</a>
    
    @endforeach
    

    Feed which whose example I shown and also reference

    https://packagist.org/packages/willvincent/feeds