PHP client library for the Guardian APIs. See documentation here. All available API modules are supported - Content, Tags, Sections, Editions, and Single Item.
You can install this package with composer using the command below
composer require nonsoniyi/the-guardian-api-client
Sign up for an API key here
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();
You can reach out to us on the Google Group here https://groups.google.com/g/guardian-api-talk for support