joeybeninghove / json-api-wp-client
遵循jsonapi.org 1.0规范的WordPress客户端库,用于消费JSON API
0.1.1
2017-02-07 22:10 UTC
Requires
- php: >= 5.6
This package is not auto-updated.
Last update: 2024-09-29 02:39:18 UTC
README
这个库是一个有偏见的、基于资源的JSON API客户端,旨在遵循JSON API 1.0规范。
需求
- WordPress
用法
定义资源
base_url
指定与API交互时使用的根URL。type
是当前资源的JSON API类型
class Invoice extends Json_Api_Wp_Resource { public function __construct() { parent::__construct( "https://api.site.com/v1/", // base URL "invoices" // type ) } }
设置HTTP身份验证
需要username
,但password
是可选的,默认为空。
Json_Api_Wp_Resource::auth( "jdoe", "secret" );
API密钥示例
如果您使用的是典型的API密钥通过HTTP身份验证,以下是一个使用基类来抽象这一过程的示例。
class Base extends Json_Api_Wp_Resource { public function __construct( $type ) { parent::__construct( "http://api.site.come/v1/", $type ); } public static function set_api_key( $api_key ) { parent::auth( $apiKey ); } } class Invoice extends Base { public function __construct() { parent::__construct( "invoices" ); } } Base::set_api_key( "some secret key" ); $invoices = Invoice::get_all();
创建资源
$invoice = Invoice::create([ "description" => "T-Shirt", "total" => 10.95 ]);
更新资源
由于WordPress HTTP库不支持PATCH
,因此此库目前不支持更新资源。
获取单个资源
$invoice = Invoice::get_one( "invoice_123" );
获取所有资源
$invoices = Invoice::get_all();
删除资源
由于WordPress HTTP库不支持DELETE
,因此此库目前不支持删除资源。