nonsoniyi / the-guardian-api-client
Guardian API 的 PHP 封装器
v1.0.4
2024-05-14 21:04 UTC
Requires
- php: ^7.4 || ^8.0
- guzzlehttp/guzzle: ^7.0
Requires (Dev)
- phpunit/phpunit: ^9
This package is auto-updated.
Last update: 2024-09-14 22:00:42 UTC
README
Guardian API 的 PHP 客户端库。请参阅以下文档 此处。支持所有可用的 API 模块 - 内容、标签、版块、版次和单个项目。
安装
您可以使用以下命令使用 composer 安装此软件包
composer require nonsoniyi/the-guardian-api-client
获取 API 密钥
在此 处 注册 API 密钥
发起请求
主要的 Guardian\GuardianAPI
类是一个工厂类,用于为每个 API 模块创建对象,允许您使用所需的请求参数向任何模块发起请求。您需要首先创建该对象,然后通过对象访问您所需的 API 模块。请参阅下面的代码片段
$api = new Guardian\GuardianAPI(THE_GUARDIAN_API_KEY);
对于内容
$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();
对于标签
$response = $api->tags() ->setQuery("apple") ->setSection("technology") ->setShowReferences("all") ->fetch();
对于版块
$response = $api->sections() ->setQuery("business") ->fetch();
对于版次
$response = $api->editions() ->setQuery("uk") ->fetch();
对于单个项目
$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();
社区支持
您可以通过以下 Google Group 联系我们以获得支持 https://groups.google.com/g/guardian-api-talk