rickwest / laravel-wordpress-api
WordPress REST API (v2) 的 Laravel 只读客户端
Requires
- php: ^8.1
- illuminate/contracts: ^9.0|^10.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- guzzlehttp/guzzle: ^7.4
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
README
一个不贪心的 WordPress REST API (v2) 只读客户端。这个包是副产品,因为我发现我想用更表达性、流畅的 Laravel 风格来查询 WordPress API。
// Without the package 👎 Http::get('https://example.com/wp-json/wp/v2/posts', [ 'search' => 'potatoes', '_embed' => 1, 'orderby' => 'date', 'order' => 'desc' '_fields' => 'title', ]); // Using the package 👌 WordPress::posts() ->search('potatoes') ->embed() ->latest() ->get('title');
除了流畅的查询构建器,你还可以享受到格式良好的响应,包括分页信息。
// Without the package 👎 $response = Http::get('https://example.com/wp-json/wp/v2/posts'); $data = $response->json(); $pages = $response->header('X-WP-TotalPages'); $total = $response->header('X-WP-Total'); // Using the package 👌 $posts = WordPress::posts()->get(); // $posts [ 'data' => [...], 'meta' => [ 'pages' => 1, 'total' => 10, ], ],
安装
您可以通过 composer 安装此包
composer require rickwest/laravel-wordpress-api
然后您需要将您的 WordPress 网址添加到您的 .env
文件中
WORDPRESS_URL=https://example.com
用法
此包将单例绑定到 Laravel 服务容器,因此您可以直接从容器或通过依赖注入轻松解决 WordPress 客户端。或者,如果您喜欢更短的表达方式,此包还公开了 Facade 和辅助函数。
目前,该包支持以下 WordPress 资源:分类、评论、媒体、页面、帖子、用户。添加对其他资源的支持非常简单,但这些是我目前需要的唯一资源!有关所有可用资源的列表,请参阅https://developer.wordpress.org/rest-api/reference。我很乐意接受任何添加的 PR。
// Resolve service directly from container and access the Posts API app(WordPress::class)->posts(); // Resolve via Facade and access the Posts API WordPress::posts(); // Resolve service via helper and access the Posts API wordpress()->posts(); // Supported resources WordPress::categories(); // Access the Categories API WordPress::comments(); // Access the Comments API WordPress::media(); // Access the Media API WordPress::pages(); // Access the Pages API WordPress::posts(); // Access the Posts API WordPress::users(); // Access the Users API Wordpress::tags(); // Access the Tags API Wordpress::plugins(); // Access the Plugins API // You can also access resources as properties wordpress()->posts
检索单个资源
通过调用资源类上的 find
方法来按 ID 获取单个资源
WordPress::posts()->find(1); // All WordPress resources share a handful of global parameters. https://developer.wordpress.org/rest-api/using-the-rest-api/global-parameters/ // You can use the relevant fluent builder methods to add these to your query WordPress::posts()->embed()->fields('title')->find(1); // Some resources also accept a limited number of resource specific parameters. These can be passed as a second argument to the find method WordPress::posts()->find(1, ['password' => 'pa55w0rd']);
检索资源集合
通过在资源上调用 get
方法来检索资源集合。您可以通过各种参数控制和筛选收到的响应,https://developer.wordpress.org/rest-api/reference/。此包提供了一些流畅的构建器方法,以轻松且富有表现性地构建所需的查询。集合响应格式良好,并包含有用的分页信息。
WordPress::posts()->get(); // All WordPress resources share a handful of global parameters, https://developer.wordpress.org/rest-api/using-the-rest-api/global-parameters/, // along with a number of filtering, ordering and pagination options. You can use the relevant fluent builder methods to build your query. WordPress::posts() ->embed(array|string $relations) // Embed linked resources into response. Reduces need for extra HTTP requests for related resources ->fields(array|string $fields) // Specify a subset fields to return in a response ->with(array|string $fields) // Alias for fields method above ->page(int $page) // Current page of the collection ->perPage(int $perPage) // Maximum number of items to be returned in result set ->search(string $term) // Limit results to those matching a string ->offset(int $offset) // Offset the result set by a specific number of items ->exlclude(int|array $ids) // Ensure result set excludes specific IDs ->inlude(int|array $ids) // Limit result set to specific IDs ->orderBy(string $field, string $direction) // Sort collection by object attribute, either ascending or descending // Resources with authors ->author() // Limit result set to resources assigned to specific authors ->authorExclude() // Ensure result set excludes resources assigned to specific authors // Resources with dates ->after(Carbon $after) // Limit response to resources published after a given ISO8601 compliant date ->before(Carbon $before) // Limit response to resources published before a given ISO8601 compliant date ->latest() // Order by date, descending ->oldest() // Order by date, ascending // Resources with slugs ->slug(string $slug) // When a utility doesn't exist for the parameter ->parameter(string $key, mixed $value) // Add a custom parameter to the query // Send it! ->get(); // Conditionally adding parameters WordPress::posts() ->when($onlyIncludeTitle, function($query) { $query->fields('title'); }) ->get();
创建、更新和删除资源
虽然此包主要用于从 WordPress API 读取数据,但您可以使用资源类上的 send
方法执行写操作。
WordPress::posts()->send(string $method, int $id, array $options); // For example, updating a post might look like... WordPress::posts()->send('POST', 1, [ 'json' => ['title' => 'My New Title'], ]);
测试
composer test
变更日志
有关最近更改的更多信息,请参阅CHANGELOG。
贡献
有关详细信息,请参阅CONTRIBUTING。
安全漏洞
有关如何报告安全漏洞的详细信息,请参阅我们的安全策略。
鸣谢
许可
MIT 许可证 (MIT)。有关更多信息,请参阅许可文件。