kirill-latish/laravel-newsapi

Laravel wrapper for https://newsapi.org/

1.0 2018-05-04 14:09 UTC

This package is not auto-updated.

Last update: 2024-05-02 16:25:01 UTC


README

Laravel wrapper for NewsAPI.org API calls. Documentation for the API can be found here

Installation

1- Require the package via Composer in your composer.json.

{
  "require": {
    "kirill-latish/laravel-newsapi": "^1.0"
  }
}

2- Run Composer to install or update the new requirement.

$ composer install

or

$ composer update

3- Add the service provider to your app/config/app.php file

NewsAPI\NewsAPIServiceProvider::class,

4- Add the facade to your app/config/app.php file

'NewsAPI' => NewsAPI\Facades\NewsAPI::class,

5- Publish the configuration file

$ php artisan vendor:publish --provider="NewsAPI\NewsAPIServiceProvider"

6- Review the configuration file and add your key (preferably through env: 'api_key' => env('NEWSAPI_KEY') )

config/newsapi.php

Usage

Refer to the official docs as to which calls can be made and check the calls in traits under NewsAPI\Requests.

For example, get all sources (if using facade):

use NewsAPI;

...

$response = NewsAPI::sources()->all();

The above returns an object containing a sources array.

Get a top headlines by country and category:


$response = NewsAPI::topHeadlines()->get([
            'country' => 'gb',
            'category'=>'sports'
        ]);

Get all news from bbc.co.uk in English for time period from 2018-05-01 to 2018-05-04 sorted by publication date:

        $response = NewsAPI::everything()->get([
            'language' => 'en',
            'domains'=>'bbc.co.uk',
            'from' => '2018-05-01',
            'to' => ''2018-05-04,
            'sortBy' => 'publishedAt',
        ]);