Laravel Package for NewsAPI.org
Laravel wrapper for NewsAPI.org API calls.
Documentation for the API can be found here, (*1)
Installation
1- Require the package via Composer in your composer.json
., (*2)
{
"require": {
"kirill-latish/laravel-newsapi": "^1.0"
}
}
2- Run Composer to install or update the new requirement., (*3)
$ composer install
or, (*4)
$ composer update
3- Add the service provider to your app/config/app.php
file, (*5)
NewsAPI\NewsAPIServiceProvider::class,
4- Add the facade to your app/config/app.php
file, (*6)
'NewsAPI' => NewsAPI\Facades\NewsAPI::class,
5- Publish the configuration file, (*7)
$ php artisan vendor:publish --provider="NewsAPI\NewsAPIServiceProvider"
6- Review the configuration file and add your key (preferably through env: 'api_key' => env('NEWSAPI_KEY')
), (*8)
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., (*9)
For example, get all sources (if using facade):, (*10)
use NewsAPI;
...
$response = NewsAPI::sources()->all();
The above returns an object containing a sources
array., (*11)
Get a top headlines by country and category:, (*12)
$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:, (*13)
$response = NewsAPI::everything()->get([
'language' => 'en',
'domains'=>'bbc.co.uk',
'from' => '2018-05-01',
'to' => ''2018-05-04,
'sortBy' => 'publishedAt',
]);