PHPackages                             rmanara/news-api-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. rmanara/news-api-php

ActiveLibrary[API Development](/categories/api)

rmanara/news-api-php
====================

PHP SDK for the NewsAPI service

19PHPCI passing

Since Apr 1Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/rodrigomanara/News-API-php)[ Packagist](https://packagist.org/packages/rmanara/news-api-php)[ RSS](/packages/rmanara-news-api-php/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (3)DependenciesVersions (2)Used By (0)

News API SDK for PHP
====================

[](#news-api-sdk-for-php)

[![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://camo.githubusercontent.com/be8249c976aa84520846711af4775a633e36db76369f1ba89c46a0e5f0eb830b/68747470733a2f2f706f7365722e707567782e6f72672f726d616e6172612f6e6577732d6170692d7068702f762f737461626c65)](https://packagist.org/packages/rmanara/news-api-php)[![License](https://camo.githubusercontent.com/fe6de41f87a57054bba87317baa733a360ce2c509b2c81eca8cbf23323fa74fd/68747470733a2f2f706f7365722e707567782e6f72672f726d616e6172612f6e6577732d6170692d7068702f6c6963656e7365)](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
------------

[](#requirements)

RequirementVersionPHP`>= 7.4`ext-curlanyext-jsonany---

Installation
------------

[](#installation)

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

---

Quick start
-----------

[](#quick-start)

```
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
-----------

[](#constructor)

```
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
---------

[](#endpoints)

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

ConstantEndpointRequired 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
--------

[](#examples)

### Top headlines

[](#top-headlines)

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

### Search everything

[](#search-everything)

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

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

### Discover sources

[](#discover-sources)

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

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

---

Error handling
--------------

[](#error-handling)

### Local validation errors

[](#local-validation-errors)

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

```
// 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

[](#transport-errors)

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

```
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
-----------------

[](#running-the-tests)

```
composer test
```

---

License
-------

[](#license)

MIT © [Rodrigo Manara](https://github.com/rodrigomanara)

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance60

Regular maintenance activity

Popularity6

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity13

Early-stage or recently created project

 Bus Factor1

Top contributor holds 85.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/0823402b5d6e295e3ebb062e139bffb741c23dfee95aadac8130f64236f6ac21?d=identicon)[rodrigomanara](/maintainers/rodrigomanara)

---

Top Contributors

[![rodrigomanara](https://avatars.githubusercontent.com/u/5967686?v=4)](https://github.com/rodrigomanara "rodrigomanara (30 commits)")[![News-API-gh](https://avatars.githubusercontent.com/u/32527401?v=4)](https://github.com/News-API-gh "News-API-gh (4 commits)")[![rodrigo-brave-agency](https://avatars.githubusercontent.com/u/33290707?v=4)](https://github.com/rodrigo-brave-agency "rodrigo-brave-agency (1 commits)")

### Embed Badge

![Health badge](/badges/rmanara-news-api-php/health.svg)

```
[![Health](https://phpackages.com/badges/rmanara-news-api-php/health.svg)](https://phpackages.com/packages/rmanara-news-api-php)
```

###  Alternatives

[stripe/stripe-php

Stripe PHP Library

4.0k143.3M480](/packages/stripe-stripe-php)[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M272](/packages/twilio-sdk)[knplabs/github-api

GitHub API v3 client

2.2k15.8M187](/packages/knplabs-github-api)[facebook/php-business-sdk

PHP SDK for Facebook Business

90121.9M34](/packages/facebook-php-business-sdk)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

73813.7M114](/packages/meilisearch-meilisearch-php)[google/gax

Google API Core for PHP

263103.1M454](/packages/google-gax)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
