reliefweb / api-php-client
该软件包最新版本(v1.5.0)没有提供许可信息。
ReliefWeb API的非常简单的PHP客户端。
v1.5.0
2020-02-05 03:35 UTC
Requires
- php: >=5.3.3
Requires (Dev)
- drupal/coder: 8.3.*
- phpunit/phpunit: 8.3.*
This package is auto-updated.
Last update: 2024-09-18 11:17:14 UTC
README
ReliefWeb API的非常简单的PHP客户端(v1)。
API URL
API 文档
请参阅ReliefWeb API文档。
安装
运行composer install --no-dev -o
。
使用方法
请参阅下面的示例,并阅读src/RWAPIClient中的函数详情。
示例
// Include autoloader. include 'vendor/autoload.php'; // Create a client. $client = new \RWAPIClient\Client(); // Set the name of the application or website using the API. $client->appname('example.com'); // Create a query to a resource. $query = new \RWAPIClient\Query('reports', $client); // Set the number of document to return. $query->limit(10); // Set the fields to include in the results. $query->fields(array('title', 'theme', 'country', 'source')); // Set the how the results should be sorted. $query->sort('date', 'desc'); // Add a query string on the title and body fields. $query->search('humanitarian', array('title', 'body')); // Create a conditional filter. $filter = new \RWAPIClient\Filter(); $filter->condition('date', array('from' => '2013-10-01T00:00:00+00:00')); // Create a nested filter. $nested_filter = new \RWAPIClient\Filter('OR'); $nested_filter->condition('theme', array('agriculture', 'health'), 'AND'); $nested_filter->condition('source', array('ocha', 'unhcr'), 'OR'); $filter->filter($nested_filter); // Add the fitler to the query. $query->filter($filter); // Create a facet on countries ordered by name. $facet = new \RWAPIClient\Facet('country', 'country', 250); $facet->sort('value', 'asc'); // Add the country facet to the query. $query->facets($facet); // Create a facet on sources ordered by count (10 most). $facet = new \RWAPIClient\Facet('organization', 'source', 10); // Add the souce facet to the query. $query->facets($facet); // Run the query. $results = $query->execute(); // Display the title of the returned resource items. $items = $results->items(); foreach ($items as $item) { echo $item['fields']['title'] . "\n"; } // Display the source facet items. $organizations = $results->facet('organization'); foreach ($organizations['data'] as $organization) { echo $organization['value'] . ' - ' . $organization['count'] . "\n"; }
获取单个资源项
// Include autoloader. include 'vendor/autoload.php'; // Create a client. $client = new \RWAPIClient\Client(); // Set the name of the application or website using the API. $client->appname('example.com'); // Create a query to a resource. $query = new \RWAPIClient\Query('reports', $client); // Get the resource item (with minimal profile). $item = $query->id(548925)->profile('minimal')->execute()->item(); // Display the item title. echo $item['fields']['title'] . "\n";
超媒体
ReliefWeb API包含超媒体链接,这在探索API资源时很有用。示例
{ "href": "https://api.reliefweb.int/v1/reports/2905933" }
可以通过设置来启用
$client->hypermedia(TRUE);
注意:这些超媒体链接最初总是由该客户端返回,但未在任何地方使用。因此,现在默认关闭以减少带宽使用。
依赖项
确保通过运行composer install --no-dev -o
提交时没有添加开发依赖项。