PHP code example of gnews-io / gnews-io-php

1. Go to this page and download the library: Download gnews-io/gnews-io-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gnews-io / gnews-io-php example snippets



ent = new \GNews\GNews('YOUR_API_KEY');

// Search for articles
$articles = $client->search('bitcoin', [
    'lang' => 'en',                   // Optional, languages of articles 
    'country' => 'us',                // Optional, country of origin of the source
    'max' => 10,                      // Optional, maximum number of articles to be returned
    'from' => '2025-01-01T00:00:00Z', // Optional, minimum publication date (

// Get the top headlines
$articles = $client->getTopHeadlines([
    'category' => 'technology'        // Optional, desired category
    'lang' => 'en',                   // Optional, languages of articles
    'country' => 'us',                // Optional, country of origin of the source
    'max' => 10,                      // Optional, maximum number of articles to be returned
    // ..., any additional parameter specified in the documentation (see https://docs.gnews.io)
]);

echo "---\n";
foreach ($articles as $article) {
    echo $article->getTitle() . "\n";
    echo $article->getSourceName() . "\n";
    echo $article->getUrl() . "\n";
    // ...
    echo "---\n";
}