Skip to content

Commit

Permalink
Upload commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kislota committed Apr 19, 2021
0 parents commit 0597508
Show file tree
Hide file tree
Showing 10 changed files with 550 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
.DS_Store
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Joseph Cobhams

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
113 changes: 113 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
## NewsAPI-PHP
A PHP client for the [News API](https://newsapi.org/docs/get-started).

### Installation
Available for installation on packagist using composer.
```
composer require jcobhams/newsapi
```

### Usage
After installation and and `require`ing `vendor/autoload.php` file in your project,

Get Your API key from [here](https://newsapi.org/register)
```php
use jcobhams\NewsApi\NewsApi;
.
.
.
$newsapi = new NewsApi($your_api_key);
```

### Get TopHeadLines
```
$newsapi->getTopHeadlines($q, $sources, $country, $category, $page_size, $page);
* $q : Keywords or a phrase to search for.
* $sources: A comma-seperated string of identifiers for the news sources or blogs you want headlines from.
Use the getSources() method to locate these programmatically or look at the sources index.
Note: you can't mix this param with the country or category params.
* $country: The 2-letter ISO 3166-1 code of the country you want to get headlines for.
Use the getCountries() method to locate these programmatically.
Note: you can't mix this param with the sources param.
* $category: The category you want to get headlines for. Use the getCategories() method to locate these programmatically.
Note: you can't mix this param with the sources param.
* $page_size: The number of results to return per page (request). 20 is the default, 100 is the maximum.
* $page: Use this to page through the results if the total results found is greater than the page size.
Returns JSON object is successful or throws excpetions if invalid data or unsuccessful request.
```

### Get Everything
```
$newsapi->getEverything($q, $sources, $domains, $exclude_domains, $from, $to, $language, $sort_by, $page_size, $page);
* $domains: A comma-seperated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com) to restrict the search to.
* $exclude_domains: A comma-seperated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com) to remove from the results.
* $from: A date and optional time for the oldest article allowed.
This should be in ISO 8601 format (e.g. 2018-11-16 or 2018-11-16T16:19:03)
Default: the oldest according to your plan.
* $to: A date and optional time for the newest article allowed.
This should be in ISO 8601 format (e.g. 2018-11-16 or 2018-11-16T16:19:03)
Default: the newest according to your plan.
* $language: The 2-letter ISO-639-1 code of the language you want to get headlines for.
Possible options: ar de en es fr he it nl no pt ru se ud zh .
Default: all languages returned. Use the getLanguages() method to locate these programmatically.
* $sort_by: The order to sort the articles in. Use the getSortBy() method to locate these programmatically.
Returns JSON object is successful or throws excpetions if invalid data or unsuccessful request.
```

### Get New Sources
```
$newsapi->getSources($category, $language, $country)
Returns JSON object is successful or throws excpetions if invalid data or unsuccessful request.
```

### Get Countries
```
$newsapi->getCountries()
Returns an array of allowed countries
```

### Get Languages
```
$newsapi->getLanguages()
Returns an array of allowed languages
```

### Get Categories
```
$newsapi->getCategories()
Returns an array of allowed categories
```

### Get SortBy
```
$newsapi->getSortBy()
Returns an array of allowed sort_by
```

### CONTRIBUTORS

This package is authored by Joseph Cobhams.

### TODO
Write more unit tests, mocks and stubs.

25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "jcobhams/newsapi",
"description": "A PHP client for NewsApi",
"keywords": ["NewsApi"],
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Joseph Cobhams",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.6",
"guzzlehttp/guzzle": "^7.2"
},
"require-dev": {
"phpunit/phpunit": "^6.*"
},
"autoload": {
"psr-4": {
"jcobhams\\NewsApi\\": "src/"
}
}
}
12 changes: 12 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<phpunit bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="true">
<testsuites>
<testsuite name="Unit Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
67 changes: 67 additions & 0 deletions src/Helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace jcobhams\NewsApi;

/**
* @author Joseph Cobhams
*/

final class Helpers
{
private static $countries = array(
'ae', 'ar', 'at', 'au', 'be', 'bg', 'br', 'ca', 'ch', 'cn', 'co', 'cu', 'cz', 'de', 'eg', 'fr', 'gb', 'gr',
'hk', 'hu','id','ie','il','in','it','jp','kr','lt','lv','ma','mx','my','ng','nl','no','nz','ph','pl', 'pt',
'ro','rs','ru','sa','se','sg','si','sk','th','tr','tw','ua','us','ve','za');

private static $languages = array('ar','en','cn','de','es','fr','he','it','nl','no','pt','ru','sv','ud');
private static $categories = array('business', 'entertainment', 'general', 'health', 'science', 'sports', 'technology');
private static $sort = array('relevancy', 'popularity', 'publishedAt');

final static public function topHeadlinesUrl($params=null){
if(!is_null($params)){
return "https://newsapi.org/v2/top-headlines?{$params}";
}
return 'https://newsapi.org/v2/top-headlines';
}

final static public function everythingUrl($params=null){
if(!is_null($params)){
return "https://newsapi.org/v2/everything?{$params}";
}
return 'https://newsapi.org/v2/everything';
}

final static public function sourcesUrl($params=null){
if(!is_null($params)){
return "https://newsapi.org/v2/sources?{$params}";
}
return 'https://newsapi.org/v2/sources';
}

final static public function isCountryValid($country){
if(in_array($country, Helpers::$countries)){ return true; }
return false;
}

final static public function isLanguageValid($language){
if(in_array($language, Helpers::$languages)){ return true; }
return false;
}

final static public function isCategoryValid($category){
if(in_array($category, Helpers::$categories)){ return true; }
return false;
}

final static public function isSortByValid($sort_by){
if(in_array($sort_by, Helpers::$sort)){ return true; }
return false;
}

final static public function __get__($key){
if($key == 'countries'){ return Helpers::$countries;}
if($key == 'languages'){ return Helpers::$languages;}
if($key == 'categories'){ return Helpers::$categories;}
if($key == 'sort'){ return Helpers::$sort;}
}
}
Loading

0 comments on commit 0597508

Please sign in to comment.