qqruz / laranews
laravel 的新闻 API 包
dev-master
2019-11-01 18:52 UTC
This package is auto-updated.
Last update: 2024-09-29 05:48:41 UTC
README
laravel 的新闻 API 实现
安装
composer require qqruz/laranews
将您的新闻 API 密钥添加到 laravel 的 .env 文件中
NEWS_API_KEY=yourapikey
用法
// get articles $articles = Laranews::topHeadlines() ->setCategory('general') ->setCountry('us') ->get() ->articles; // get sources $sources = Laranews::sources()->get()->sources; // you can also pass arguments directly to endpoint $bitcoin = Laranews::everything([ 'q' => 'bitcoin', 'language' => 'en' ])->get();
保存和加载
Laranews 提供用于保存文章、来源和请求的 Eloquent 模型,以及相应的迁移文件。要使用自定义模型,请编辑配置文件。
use QQruz\Laranews\Article; use QQruz\Laranews\Request; use QQruz\Laranews\Source; Laranews::headlines()->setCategory('general')->get()->save('myApiCall'); // load articles $articles = Article::all()->get(); // load request $headlinesGeneral = Laranews::load('myApiCall'); // or $requests = Request::where('endpoint', 'everything')->get(); foreach ($requests as $request) { // same as Laranews::load($request)->get()->save(); Laranews::update($request); }
可用方法
端点
为选定的端点设置参数。以下方法中的任何一个都应首先调用
headlines() / topHeadlines()
everything()
sources()
// sets up for top-headlines endpoint Laranews::headlines();
设置 URL 参数
为 API 调用设置 URL 参数。有关更多信息,请参阅新闻 API 的文档。
set{$property}($value)
// sets up for sources endpoint, entertainment only, from Serbia $sources = Laranews::sources()->setCategory('entertainment')->setCountry('rs');
获取 URL 参数
get{$property}($value)
$country = $news->getCountry() // returns Serbia
获取结果
get()
// sources $sources->get(); $sources = $news->sources; //articles $articles = $articles->get()->articles;
保存
保存请求和结果
// optional name is for identifing the request
// if not provided it will be auto generated $endpoint + time()
save($name)
保存文章 + 来源
saveResults()
仅保存请求
saveRequest($name)
仅保存文章
saveArticles()
仅保存来源
saveSources()
// only articles will be saved // results from sources endpoint will be ignored Laranews::everything()->setQInTitle('laravel')->get() ->sources()->get()->saveArticles();
加载
从保存的模型中加载请求。如果提供字符串,它将查找具有该名称的模型。如果提供整数,它将查找具有该 ID 的模型。
load($request)
use QQruz\Laranews\Request; $request = Request::where('name', 'someName'); $news = Laranews::load($request) // or $news = Laranews::load('someName'); // or $news = Laranews::load(1); // additionally you can load request from custom model $news = Laranews::fromModel(Some\Model\Name)
自动更新
Laranews 提供两种自动更新方式
- 中间件
- 定时任务
要启用自动更新保存的请求,您需要调用
auto() or autoUpdate()
Laranews::topHeadlines([ 'category' => 'health' ])->autoUpdate()->saveRequest('doctors');
中间件
您可以将更新触发器添加到特定路由或组。传递的参数表示两次更新之间的最小时间,这样可以节省 API 调用。
Route::get('/', function () { // update every 15 minutes all requests with auto_update flag })->middleware('laranews:15');
第二个可选参数可以指定要运行的请求(请求)。请注意,只有指定的请求将运行,其余的将被忽略。
Route::get('/news/business', function () { // update every 15 minutes request with ID 4 })->middleware('laranews:15,4'); Route::get('/news/health', function () { // update every 15 minutes request named doctors })->middleware('laranews:15,doctors'); Route::get('/news/business_doctors', function () { // updates both every 30 min })->middleware('laranews:30,doctors,4');
定时任务调度器
目前,您只能使用定时任务来更新所有标记的请求。要启用定时任务,您首先需要编辑配置文件
'schedule' => [ 'enabled' => true, 'method' => 'everyFifteenMinutes', 'params' => null ]
有关可用方法和参数,请访问 https://laravel.net.cn/docs/5.8/scheduling#schedule-frequency-options
GUI
GUI 尚在开发中,不建议在生产环境中使用,但它可以帮助您入门。它包括两个 Bootstrap 主题文件
- laranews::builder - 用于创建新请求的表单
- laranews::listing - 保存请求的列表和编辑
要使这些表单工作,您需要编辑配置文件。