Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b439ce4
simple api call using cUrl
rodrigomanara Mar 20, 2018
0cff753
add composer
rodrigomanara Mar 20, 2018
725d331
remove extra para demo
rodrigomanara Mar 20, 2018
d4ceac6
remove ide config
rodrigomanara Mar 20, 2018
93e309b
Update README.md
rodrigomanara Mar 20, 2018
d6974f9
Update README.md
rodrigomanara Mar 20, 2018
873fa00
Update README.md
rodrigomanara Mar 20, 2018
08e4fe2
Update README.md
rodrigomanara Mar 20, 2018
bc2e16b
add changes and fix issue on interface
rodrigomanara Mar 21, 2018
18944e9
Merge branch 'master' of https://github.com/rodrigomanara/News-API-php
rodrigomanara Mar 21, 2018
b5f81e7
add validation on apikey
rodrigo-brave-agency Mar 21, 2018
26e38cb
Merge branch 'master' into master
rodrigomanara Jul 23, 2019
5afc1ee
Create .travis.yml
rodrigomanara Jul 23, 2019
2a7d187
Create testApi.php
rodrigomanara Jul 23, 2019
b45e954
Create phpunit.xml
rodrigomanara Jul 23, 2019
04a6ca7
Update phpunit.xml
rodrigomanara Jul 23, 2019
8582cd9
add changes on test
rodrigomanara Jul 23, 2019
0cce3fa
fix path
rodrigomanara Jul 23, 2019
4c611e2
fix path
rodrigomanara Jul 23, 2019
00d071a
fix phpunit
rodrigomanara Jul 23, 2019
dbf6e1a
Update README.md
rodrigomanara Jul 23, 2019
f2b222c
Update README.md
rodrigomanara Jul 23, 2019
a857fe3
Update README.md
rodrigomanara Jul 23, 2019
2e81ef6
Update README.md
rodrigomanara Jul 23, 2019
3689e86
Create php.yml
rodrigomanara Dec 21, 2019
58071e6
Update composer require command for news-api-php
rodrigomanara Apr 1, 2026
a2cedce
implementations
rodrigomanara Apr 2, 2026
d480550
add changes on gitignore
rodrigomanara Apr 2, 2026
3449e62
update read me
rodrigomanara Apr 2, 2026
56d9e81
correct agents
rodrigomanara Apr 2, 2026
70ec668
bug on call agent setting
rodrigomanara Apr 2, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: PHP Composer

on: [push]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1

- name: Validate composer.json and composer.lock
run: composer validate

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
# Docs: https://getcomposer.org/doc/articles/scripts.md

# - name: Run test suite
# run: composer run-script test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ Homestead.json

# Rocketeer PHP task runner and deployment package. https://github.com/rocketeers/rocketeer
.rocketeer/
/nbproject/private/
/nbproject/

.idea
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"testApi::testwithKeywithrequiredParametersApiCall":4,"testApi::testwithoutKeyApiCall":4},"times":{"testApi::testwithoutKeyApiCall":0.002,"testApi::testwithKeywithoutrequiredParametersApiCall":0.164,"testApi::testwithKeywithrequiredParametersApiCall":0.002,"testApi::testWithInvalidTypeReturnsError":0.002}}
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: php
php:
- '7.3'
sudo: required
before_script:
- composer install --prefer-source
notifications:
email:
- me@rodrigomanara.co.uk
165 changes: 163 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,165 @@
# News API SDK for PHP
Coming soon... this is where our officially supported SDK for PHP is going to live.

***
[![PHP Composer](https://github.com/rodrigomanara/News-API-php/actions/workflows/php.yml/badge.svg)](https://github.com/rodrigomanara/News-API-php/actions/workflows/php.yml)
[![Latest Stable Version](https://poser.pugx.org/rmanara/news-api-php/v/stable)](https://packagist.org/packages/rmanara/news-api-php)
[![License](https://poser.pugx.org/rmanara/news-api-php/license)](https://packagist.org/packages/rmanara/news-api-php)

A lightweight PHP SDK for the [NewsAPI v2](https://newsapi.org/docs/) service.
Search through millions of articles from over 30,000 news sources and blogs — including breaking news and niche publications.

---

## Requirements

| Requirement | Version |
|---|---|
| PHP | `>= 7.4` |
| ext-curl | any |
| ext-json | any |

---

## Installation

```bash
composer require rmanara/news-api-php:^1.0
```

---

## Quick start

```php
require_once __DIR__ . '/vendor/autoload.php';

$api = new \NewsApi\Api('YOUR_API_KEY', ['q' => 'PHP', 'language' => 'en']);
$data = $api->getData();

// $data is a stdClass decoded from the JSON response.
echo $data->totalResults;
foreach ($data->articles as $article) {
echo $article->title . PHP_EOL;
}
```

> **Security note:** the API key is transmitted via the `X-Api-Key` request header
> and is never appended to the URL. This keeps it out of server access logs,
> browser history, and HTTP Referer headers.

---

## Constructor

```php
new \NewsApi\Api(
string $apiKey, // Required. Key from newsapi.org.
array $query = [], // Endpoint query parameters.
string $type = enumType::TOP_HEADLINE // Endpoint type constant.
)
```

Validation runs before any network request is made, in this order:

1. `$apiKey` must be non-empty.
2. `$type` must be one of the `enumType` constants.
3. `$query` must contain at least one parameter.

If any check fails, `getData()` returns a local error array and no HTTP call is made.

---

## Endpoints

Use the `\NewsApi\enumType` constants to select an endpoint:

| Constant | Endpoint | Required query params |
|---|---|---|
| `enumType::TOP_HEADLINE` *(default)* | `top-headlines` | one of: `sources`, `q`, `language`, `country` |
| `enumType::EVERYTHING` | `everything` | one of: `q`, `sources`, `domains` |
| `enumType::SOURCES` | `sources` | none — all params optional |

---

## Examples

### Top headlines

```php
$api = new \NewsApi\Api('YOUR_API_KEY', ['country' => 'gb']);
$data = $api->getData();
```

### Search everything

```php
use NewsApi\Api;
use NewsApi\enumType;

$api = new Api('YOUR_API_KEY', ['q' => 'climate change', 'language' => 'en'], enumType::EVERYTHING);
$data = $api->getData();
```

### Discover sources

```php
use NewsApi\Api;
use NewsApi\enumType;

$api = new Api('YOUR_API_KEY', ['language' => 'en', 'country' => 'us'], enumType::SOURCES);
$sources = $api->getData();
```

---

## Error handling

### Local validation errors

When a validation guard fails before any request is made, `getData()` returns an
associative array:

```php
// Missing or empty API key
$api = new \NewsApi\Api('', ['q' => 'test']);
$data = $api->getData();
// ['error' => ['apikey' => 'missing apikey']]

// Unsupported endpoint type
$api = new \NewsApi\Api('YOUR_API_KEY', ['q' => 'test'], 'bad-type');
$data = $api->getData();
// ['error' => ['type' => 'type is not correct']]

// Empty query array
$api = new \NewsApi\Api('YOUR_API_KEY', []);
$data = $api->getData();
// ['error' => ['query' => 'empty query']]
```

### Transport errors

A `\RuntimeException` is thrown when cURL fails (e.g. DNS resolution error,
connection timeout):

```php
try {
$api = new \NewsApi\Api('YOUR_API_KEY', ['q' => 'PHP']);
$data = $api->getData();
} catch (\RuntimeException $e) {
// Handle transport failure
echo $e->getMessage();
}
```

---

## Running the tests

```bash
composer test
```

---

## License

MIT © [Rodrigo Manara](https://github.com/rodrigomanara)
60 changes: 60 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "rmanara/news-api-php",
"description": "PHP SDK for the NewsAPI service",
"version": "1.0.0",
"type": "library",
"license": "MIT",
"homepage": "https://github.com/rodrigomanara/News-API-php",
"keywords": [
"news",
"newsapi",
"sdk",
"api",
"php"
],
"authors": [
{
"name": "Rodrigo Manara",
"email": "me@rodrigomanara.co.uk"
}
],
"support": {
"issues": "https://github.com/rodrigomanara/News-API-php/issues",
"source": "https://github.com/rodrigomanara/News-API-php"
},
"minimum-stability": "stable",
"prefer-stable": true,
"require": {
"php": ">=7.4",
"ext-curl": "*",
"ext-json": "*"
},
"autoload": {
"psr-4": {
"NewsApi\\": "src/"
}
},
"autoload-dev": {
"classmap": [
"test/"
]
},
"require-dev": {
"phpunit/phpunit": "^8.5.52"
},
"scripts": {
"test": "vendor/bin/phpunit",
"test:coverage": "vendor/bin/phpunit --coverage-text"
},
"config": {
"sort-packages": true,
"optimize-autoloader": true,
"preferred-install": "dist",
"allow-plugins": {}
},
"extra": {
"branch-alias": {
"dev-main": "1.0.x-dev"
}
}
}
Loading