lambry / apison
WordPress 插件,用于获取、缓存和访问 API 数据。
v0.2.1
2019-09-14 05:11 UTC
Requires
- php: >7.1.0
This package is auto-updated.
Last update: 2024-09-29 05:02:42 UTC
README
Apison 是一个小型的 WordPress 插件,用于获取、缓存和访问 API 数据 (JSON)。
只需在 WordPress 管理员中添加您的 API 端点(如有适用,请添加令牌),然后在 Settings->Apison 下,您就可以通过辅助类或 REST 端点访问缓存的 API 数据。
安装
运行 composer require lambry/apison
或直接下载此存储库并通过 WordPress 管理员安装。
管理员界面
通过 PHP 获取缓存的 API 数据
<?php use Lambry\Apison\Frontend\Api; // Get all jobs Api::get('jobs')->all(); // Get full forcast with just humidity and temperature fields Api::get('forcast')->with(['humidity', 'temperature'])->all(); // Get all contacts with a role of either sales or marketing Api::get('contacts')->where('role', ['sales', 'marketing'])->all(); // Get the last 10 listings that have a price greater than 100 Api::get('listings')->where('price', 'gt', 100)->last(10); // Get the first 20 events offset by 20 that are not in the sports category Api::get('events')->where('category', 'not', 'sports')->first(20, 20); // Get the title and price for all products that are currently on sale and are priced under 50 Api::get('products')->where('sale', true)->and('price', 'lt', 50)->with(['title', 'price'])->all();
通过 REST 端点获取缓存的 API 数据
<!-- Get all jobs --> /wp-json/apison/jobs <!-- Get full forcast with just humidity and temperature fields --> /wp-json/apison/forcast?with=humidity,temperature <!-- Get all contacts with a role of either sales or marketing --> /wp-json/apison/contacts?role=sales,marketing <!-- Get the last 10 listings that have a price greater than 100 --> /wp-json/apison/listings?price.gt=100&last=10 <!-- Get the first 20 events offset by 20 that are not in the sports category --> /wp-json/apison/events?category.not=sports&first=20,20 <!-- Get the title and price for all products that are currently on sale and are priced under 50 --> /wp-json/apison/products?sale=true&price.lt=50&with=title,price
过滤器
apison/key
:允许您提供 API 密钥,在添加新 URL 时,只需使用 _key_
作为占位符即可。
apison/cache
:过滤管理员缓存选择框中的可用选项,例如 15 分钟、1 小时等。
apison/permission
:设置注册管理员菜单时使用的权限。
<?php // Setting api keys add_filter('apison/key', function($slug) { switch ($slug) { case 'events': return env('EVENTS_API_KEY'); break; case 'weather': return env('WEATHER_API_KEY'); break; } }); // Adding new cache durations add_filter('apison/cache', function($options) { $options['10080'] = '1 week'; return $options; }); // Altering permissions to see the plugin admin add_filter('apison/permission', function() { return 'manage_network'; });
注意事项
接受的 where/and 条件操作符包括:is
、not
、gt
(大于)、lt
(小于)、gte
(大于或等于)和 lte
(小于或等于)。
可以通过强制刷新缓存中的任何端点来刷新,例如通过访问 refresh/endpoint,即 /wp-json/apison/refresh/jobs
或 /wp-json/apison/refresh/forecast
。
此插件需要 WordPress 4.8.0+
和 PHP 7.1.0+
。