nonsoniyi/the-guardian-api-client

PHP wrapper for the guardian APIs

v1.0.4 2024-05-14 21:04 UTC

This package is auto-updated.

Last update: 2024-09-14 22:00:42 UTC


README

PHP client library for the Guardian APIs. See documentation here. All available API modules are supported - Content, Tags, Sections, Editions, and Single Item.

Installation

You can install this package with composer using the command below

 composer require nonsoniyi/the-guardian-api-client

Get API key

Sign up for an API key here

Making Requests

The primary Guardian\GuardianAPI class is a factory class that creates objects for each of the API modules, allowing you to make requests to any of them with your desired request parameters. You have to first create an object for it, then access your desired API module via the object. See the code snippets below:

$api = new Guardian\GuardianAPI(THE_GUARDIAN_API_KEY);

For Content:

$response = $api->content()
    ->setQuery("12 years a slave")
    ->setTag("film/film,tone/reviews")
    ->setFromDate(new DateTimeImmutable("01/01/2010"))
    ->setToDate(new \DateTimeImmutable())
    ->setShowTags("contributor")
    ->setShowFields("starRating,headline,thumbnail,short-url")
    ->setOrderBy("relevance")
    ->fetch();

For Tags:

$response = $api->tags()
    ->setQuery("apple")
    ->setSection("technology")
    ->setShowReferences("all")
    ->fetch();

For Sections:

$response = $api->sections()
    ->setQuery("business")
    ->fetch();

For Editions:

$response = $api->editions()
    ->setQuery("uk")
    ->fetch();

For Single Item:

$response = $api->singleItem()
    ->setId("/sport/2022/oct/07/cricket-jos-buttler-primed-for-england-comeback-while-phil-salt-stays-focused")
    ->setShowStoryPackage(true)
    ->setShowEditorsPicks(true)
    ->setShowMostViewed(true)
    ->setShowRelated(true)
    ->fetch();

Community support

You can reach out to us on the Google Group here https://groups.google.com/g/guardian-api-talk for support