t14 / open-platform-php
用于利用《卫报》开放平台的PHP库
Requires
- guzzlehttp/guzzle: 4.*
- symfony/yaml: 2.6.7
Requires (Dev)
- phpunit/phpunit: 4.0.*
This package is not auto-updated.
Last update: 2024-10-02 08:29:50 UTC
README
#Guardian Open Platform PHP Client
这是一个基于PHP的客户端库,旨在为使用《卫报》新闻报纸的开放平台API提供一个易于使用的选项。
#开始之前 在开始之前,您需要获取一个API密钥,您可以在此处获取
##安装 使用composer composer require t14/open-platform-php 创建一个类似于下面的config.yml文件,并添加您的API密钥。
# Open Platform Config
- host:
- 'http://content.guardianapis.com'
- apiKey:
- 'YOUR API KEY HERE'
##基本用法 编辑config.yml文件,将API密钥替换为您的自己的。您可以选择使用不同的YAML配置文件(见下面的配置部分)
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();
getContent() 返回一个Guzzle客户端对象,使其能够链式调用除getBody()之外的其他方法。有关更多信息,请参阅Guzzle文档
searchQuery('my free text search') 方法允许您将自由文本搜索添加到查询中。您还可以指定过滤器并使用不同的端点。
##端点 您可以通过将端点的名称作为参数传递给endpoint('nameOfEndpoint')方法来查询不同的端点。例如,要查询端点端点,您可以执行以下操作
$op->endpoint('sections') ->searchQuery('business') ->getContent()->getBody();
##过滤器 您可以将filter方法链式调用到endpoint方法,然后到其本身,允许您设置多个过滤器。每个端点支持某些过滤器,如果尝试使用不兼容的过滤器,则会抛出异常。要检查哪些端点支持哪些过滤器,您可以使用内容探索器
过滤器方法接受一个查询作为其第一个参数,以及您想要使用的过滤器名称作为其第二个参数。例如,如果您想使用“search”端点并将结果过滤为仅显示单个页面,按“政治”部分最近的内容排序,您可以执行以下操作:
$op->endpoint('search') ->filter('politics', 'section') ->filter('newest', 'order-by') ->filter(1, 'page') ->getContent()->getBody();
##有用的资源 这个库在很大程度上基于开放平台的内容探索器。您可以使用它来帮助您找出哪些过滤器与哪些端点兼容,查看您的查询返回的结果类型,等等。open-platform.theguardian.com
##贡献 请随意fork此仓库并提交一个pull request。