Master
Dev
, (*1)
This is a PHP based client library that aims to provide an easy to use option for consuming
The Guardian NewsPapers Open Platform API., (*2)
Before you get started
Before you get started you need to get an api key which you can obtain here, (*3)
Installation
Using composer composer require t14/open-platform-php
Create a config.yml file like the one below and add your api key., (*4)
# Open Platform Config
- host:
- 'http://content.guardianapis.com'
- apiKey:
- 'YOUR API KEY HERE'
Basic Usage
Edit the config.yml file and replace the api key with your own.
You can choose to use a different YAML config file (see the config section below), (*5)
require 'vendor/autoload.php';
use OpenPlatform\Client;
$op = new Client('DIR_OF_CONFIG_FILE/config.yml');
$op->endpoint('search')
->filter('politics', 'section')
->filter('newest', 'order-by')
->searchQuery('latest election news')
->getContent()->getBody();
The getContent()
returns a Guzzle client object making it possible to chain a different method other than getBody()
See Guzzle documentation for more information, (*6)
The searchQuery('my free text search')
method allows you to add a free text search to your query. You also have the option to specify
filters and use different endpoints., (*7)
Endpoints
You can query a different endpoint by passing the name of the endpoint as a parameter to the endpoint('nameOfEndpoint')
method.
For example to query the sections endpoint you could do the following, (*8)
$op->endpoint('sections')
->searchQuery('business')
->getContent()->getBody();
Filters
You can chain the filter
method to the endpoint
method and then to its self allowing you to set multiple filters.
Each endpoint supports certain filters and an exception will be thrown if you try to use an incompatible filter to
to check which filters are supported by which endpoint you can use the content explorer, (*9)
The filter method takes a query as its first parameter and the name of the filter you want to use as its second parameter.
For example if you would like to use the 'search' endpoint and filter your results to only show a single page,
ordered by the most recent from the 'politics' section you could do the following;, (*10)
$op->endpoint('search')
->filter('politics', 'section')
->filter('newest', 'order-by')
->filter(1, 'page')
->getContent()->getBody();
Useful resources
This library is largely based on the Open Platform content explorer. You can use it
to help you find out which filters are compatible with which endpoints, see what kind of results your queries return and
more.
open-platform.theguardian.com, (*11)
Contributing
Please feel free to fork this repo and open a pull request., (*12)