PHP code example of shamithewebdeveloper / laravel-nytimes-api

1. Go to this page and download the library: Download shamithewebdeveloper/laravel-nytimes-api 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/ */

    

shamithewebdeveloper / laravel-nytimes-api example snippets


ShamiTheWebdeveloper\NYTimes\NYTimesServiceProvider::class

'aliases' => [
        'NYTimes' => \ShamiTheWebdeveloper\NYTimes\Facades\NYTimesFacade::class,
    ]

use ShamiTheWebdeveloper\NYTimes\Facades\NYTimesFacade;
public function register(): void
    {
        $loader = AliasLoader::getInstance();
        $loader->alias('NYTimes', NYTimesFacade::class);
    }

use ShamiTheWebdeveloper\NYTimes\NYTimes;

NYTimes::searchArticle('Sports', now()->lastWeekDay, now())->get();
// use get() to get response in array

NYTimes::searchArticle('Sports', now()->lastWeekDay, now())->json();
// use json() to get response in json

$articlesFullResponse=NYTimes::searchArticle('Sports', '2024-04-01', now())->fullResponse();
dd($articlesFullResponse);

$articleURL=NYTimes::searchArticle('Sports', '2024-04-01', now())->requestURL();
echo $articleURL;

@dd(NYTimes::newsSectionList()->get())

/*
Parameters
$query (string hArticle('Sports', now()->lastWeekDay, now())->get();

foreach ($articles['response']['docs'] as $article)
{
  echo $article['abstract'].'<br>';
  echo $article['web_url'].'<br>';
}

/*
Parameters
$month (int rchive(5,2025)->get();

foreach ($archives['response']['docs'] as $archive) {
    echo $archive['abstract'].'<br>';
    echo $archive['section_name'].'<br>';
    echo $archive['word_count'].'<br>';
}


/*
Parameters
$type (string tPopular('emailed',7)->get();

foreach ($populars['results'] as $popular) {
    echo $popular['section'].'<br>';
    echo $popular['title'].'<br>';
    echo $popular['abstract'].'<br>';
}

/*
Parameters
$publishedDate (string)
*/

$books=NYTimes::bookOverview('2024-05-04')->get();

echo $books['results']['published_date'].'<br>';
echo '<h3>Books:</h3>';

foreach ($books['results']['lists'] as $booklist)
{
  echo $booklist['list_name'].'<br>';

  foreach ($booklist['books'] as $book)
  {
    echo $book['title'].'<br>';
    echo $book['author'].'<br>';
  }
}

/*
Parameters
$list (string t('series-books')->get();

echo '<h2>'.$books['results']['list_name'].'</h2>';

foreach ($books['results']['books'] as $book)
{
    echo $book['title'].'<br>';
    echo $book['author'].'<br>';
}



/*
Parameters
$source (string re('nyt','all',30,20)->get();

foreach ($news['results'] as $new)
{
    echo $new['title'].'<br>';
    echo $new['abstract'].'<br>';
    echo $new['url'].'<br>';
}

/*
Parameters
$url (string),
*/

$url='https://www.nytimes.com/2025/08/25/arts/dance/kennedy-center-stephen-nakagawa.html';

$news=NYTimes::timesNewswireUrl($url)->get();

foreach ($news['results'] as $new)
{
    echo $new['title'].'<br>';
    echo $new['abstract'].'<br>';
}

$sections=NYTimes::newsSectionList()->get();

foreach ($sections['results'] as $section)
{
    echo $section['section'].'<br>';
    echo $section['display_name'].'<br>';
}

/*
Parameters
$sections (string),
*/

$stories=NYTimes::topStories('food')->get();

foreach ($stories['results'] as $story)
{
    echo $story['title'].'<br>';
    echo $story['abstract'].'<br>';
    echo $story['url'].'<br>';
}

/*
Parameters
$section (string),
*/

$feeds=NYTimes::rssFeed('Automobiles')->get();

echo $feeds['channel']['title'].'<br>';

foreach ($feeds['channel']['item'] as $feed)
{
    echo $feed['title'].'<br>';
    echo $feed['link'].'<br>';
    echo $feed['description'].'<br>';
}

// returns data in XML (toXML() Only valid for RSS Feed)
$feeds=NYTimes::rssFeed('Automobiles')->toXML();
header('Content-type: application/xml');
echo $feeds;
bash
  php artisan vendor:publish --tag=nytimes-config