How to install and use the PHP SDK
Install the PHP client and make a news request
Written by Jacob Partington
July 2, 2026
Updated October 16, 2026
How to install and use the PHP SDK
Run composer require apitube/news-api, create an APITube\Client with your key and call news('everything', $params). The SDK sends the request and returns article objects. It requires PHP 8.1+ and a compatible HTTP client.
use APITube\Client;
$client = new Client(apiKey: 'your-api-key');
$response = $client->news('everything', [
'title' => 'artificial intelligence',
'language.code' => 'en',
'per_page' => 5,
]);
foreach ($response->articles as $article) {
echo $article->title . "\n";
echo $article->url . "\n\n";
}
How do you install the PHP SDK?
Install the Composer package:
composer require apitube/news-api
The SDK needs a PSR-18 HTTP client and PSR-17 factory: standard interfaces that let you choose an HTTP library. Guzzle is one option:
composer require guzzlehttp/guzzle
Load Composer’s autoloader before using APITube\Client. The SDK can discover an installed compatible client if you do not pass one explicitly.
How do you create the client and authenticate?
Create one client and reuse it. Supply apiKey; the base URL defaults to https://api.apitube.io:
use APITube\Client;
$client = new Client(
apiKey: getenv('APITUBE_API_KEY'), // keep the key out of source
);
It sends X-API-Key for you. To set a timeout, pass a configured client:
$client = new Client(
apiKey: getenv('APITUBE_API_KEY'),
httpClient: new \GuzzleHttp\Client(['timeout' => 30]),
);
See where to find your key and authentication.
How do you search for news articles?
Pass an endpoint name and filter array to news(). Keep dotted parameter names, such as language.code, exactly as written:
$response = $client->news('everything', [
'title' => 'climate change',
'language.code' => 'en',
'sort.by' => 'published_at',
'sort.order' => 'desc',
'per_page' => 10,
]);
echo "Page: {$response->page}\n";
echo "Has next page: " . ($response->hasNextPages ? 'yes' : 'no') . "\n";
foreach ($response->articles as $article) {
echo "{$article->title}\n";
echo "Source: {$article->source?->domain}\n";
echo "Published: {$article->publishedAt}\n";
echo "Sentiment: {$article->sentiment?->overall?->polarity}\n";
}
The returned ArticleList has articles, page, limit, hasNextPages and nextPage. SDK properties include url and publishedAt; direct REST responses use href and published_at.
For a story, include id: $client->news('story', ['id' => 'story-id']). A missing ID raises InvalidArgumentException. The optional third version argument defaults to v1. See the parameter reference.
How do you count, autocomplete and read reference data?
count($params): returns the matching article count as an integer.suggest($type, $prefix): looks up categories, topics, industries or entities.people($params),companies($params)andjournalists($params): return paginatedresults. Useperson($id),company($id)orjournalist($id)for a profile array.balance(): returns aBalanceResponsewithplanandpoints.ping(): returns whether the API is reachable.
How do you handle errors and rate limits?
Catch AuthenticationException for 401, RateLimitException for 429, or the base ApiException for other API errors. retryAfter gives a wait time in seconds. ApiException provides requestId and the HTTP status through getCode():
use APITube\Exceptions\ApiException;
use APITube\Exceptions\AuthenticationException;
use APITube\Exceptions\RateLimitException;
try {
$response = $client->news('everything', ['title' => 'php']);
} catch (AuthenticationException $e) {
echo "Auth error: {$e->getMessage()}\n";
} catch (RateLimitException $e) {
echo "Rate limited. Retry after {$e->retryAfter}s\n";
} catch (ApiException $e) {
echo "API error ({$e->getCode()}): {$e->getMessage()}, request {$e->requestId}\n";
}
Keep the request ID for support and wait before retrying 429; see rate-limit errors. For JavaScript projects, see the Node.js SDK.
Common Questions
- Which PHP versions are supported?
- Do I need Guzzle specifically?
- Which endpoints can I call from the SDK?
- Can I use my own HTTP client?
Which PHP versions are supported?
PHP 8.1 or newer, as required by the package’s Composer configuration.
Do I need Guzzle specifically?
No. Use a PSR-18 client and PSR-17 factory. Guzzle is one supported option; another compatible library can work too.
Which endpoints can I call from the SDK?
news() accepts everything, top-headlines, story, article and raw. Separate methods provide counts, suggestions, directories, profiles, balance and ping.
Can I use my own HTTP client?
Yes. Pass a PSR-18 client as httpClient when creating the SDK client. Configure timeouts, middleware or proxy settings on that HTTP client.