jcobhams / newsapi
NewsApi 的 PHP 客户端
v1.0.1
2023-02-19 16:24 UTC
Requires
- php: >=5.6
- guzzlehttp/guzzle: 6 - 7
Requires (Dev)
- phpunit/phpunit: 6 - 9
This package is auto-updated.
Last update: 2024-09-19 19:43:04 UTC
README
这是一个为 News API 编写的 PHP 客户端。
安装
可以使用 composer 在 Packagist 上安装。
composer require jcobhams/newsapi
使用方法
在项目安装后,并在项目中 require
vendor/autoload.php
文件,
从 这里 获取您的 API 密钥
use jcobhams\NewsApi\NewsApi; . . . $newsapi = new NewsApi($your_api_key);
获取头条新闻
$newsapi->getTopHeadlines($q, $sources, $country, $category, $page_size, $page);
* $q : Keywords or a phrase to search for.
* $sources: A comma-seperated string of identifiers for the news sources or blogs you want headlines from.
Use the getSources() method to locate these programmatically or look at the sources index.
Note: you can't mix this param with the country or category params.
* $country: The 2-letter ISO 3166-1 code of the country you want to get headlines for.
Use the getCountries() method to locate these programmatically.
Note: you can't mix this param with the sources param.
* $category: The category you want to get headlines for. Use the getCategories() method to locate these programmatically.
Note: you can't mix this param with the sources param.
* $page_size: The number of results to return per page (request). 20 is the default, 100 is the maximum.
* $page: Use this to page through the results if the total results found is greater than the page size.
Returns JSON object is successful or throws excpetions if invalid data or unsuccessful request.
获取所有内容
$newsapi->getEverything($q, $sources, $domains, $exclude_domains, $from, $to, $language, $sort_by, $page_size, $page);
* $domains: A comma-seperated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com) to restrict the search to.
* $exclude_domains: A comma-seperated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com) to remove from the results.
* $from: A date and optional time for the oldest article allowed.
This should be in ISO 8601 format (e.g. 2018-11-16 or 2018-11-16T16:19:03)
Default: the oldest according to your plan.
* $to: A date and optional time for the newest article allowed.
This should be in ISO 8601 format (e.g. 2018-11-16 or 2018-11-16T16:19:03)
Default: the newest according to your plan.
* $language: The 2-letter ISO-639-1 code of the language you want to get headlines for.
Possible options: ar de en es fr he it nl no pt ru se ud zh .
Default: all languages returned. Use the getLanguages() method to locate these programmatically.
* $sort_by: The order to sort the articles in. Use the getSortBy() method to locate these programmatically.
Returns JSON object is successful or throws excpetions if invalid data or unsuccessful request.
获取新的来源
$newsapi->getSources($category, $language, $country)
Returns JSON object is successful or throws excpetions if invalid data or unsuccessful request.
获取国家
$newsapi->getCountries()
Returns an array of allowed countries
获取语言
$newsapi->getLanguages()
Returns an array of allowed languages
获取分类
$newsapi->getCategories()
Returns an array of allowed categories
获取排序方式
$newsapi->getSortBy()
Returns an array of allowed sort_by
贡献者
此包由 Joseph Cobhams 编写。
待办事项
编写更多的单元测试、模拟和存根。