angelxmoreno/wprestclient

WPRestClient 是一个用于通过 RESTful API 无缝交互 WordPress 网站的 PHP 库

v1.2.1 2024-07-22 15:32 UTC

This package is auto-updated.

Last update: 2024-09-22 15:51:32 UTC


README

Codacy Badge codecov GitHub license Open Source? Yes! PHP Version 7.4+ Maintenance

WPRestClient Logo

PHP WordPress API 客户端

WPRestClient 是一个用于通过 WordPress RESTful API 无缝与 WordPress 网站交互的 PHP 库,简化了认证、数据检索和内容管理任务

功能

要求

PHP 7.4+

安装

您可以通过 Composer 安装 WPRestClient。在您的项目目录中运行以下命令

composer require angelxmoreno/wprestclient

使用方法

  1. 创建 WPRestClient 的新实例

    use WPRestClient\Core\ApiClient;
    
    $client = new ApiClient('https://example.com');
  2. 通过客户端检索帖子原始数组

    $posts = $client->sendRequest('get','/posts');
    foreach ($posts as $post) {
        echo $post['title']['rendered'];
    }
  3. 通过 PostsRepository 检索 PostEntity 对象数组

    use WPRestClient\Repository\PostsRepositor;
    
    PostsRepository::setApiClient($client);
    $posts = PostsRepository::fetch();
    foreach ($posts as $post) {
       echo $post->getTitle();
    }
  4. 使用 RepositoryRegistry 通过 PagesRepository 创建新的页面:通过创建 RepositoryRegistry 的实例,您就可以访问所有已注册的存储库,并且 ApiClient 已经设置好了。

    use WPRestClient\Repository\PagesRepository;
    use WPRestClient\Core\RepositoryRegistry;
    use WPRestClient\Entity\PageEntity;
    
    $registry = new RepositoryRegistry($client);
    $page = new PageEntity(['title' => 'A New Page']);
    $registry->pages()->create($page);
  5. 使用 RepositoryRegistry 通过 PostsRepository 删除帖子

    use WPRestClient\Repository\PagesRepository;
    use WPRestClient\Core\RepositoryRegistry;
    use WPRestClient\Entity\PageEntity;
    
    $registry = new RepositoryRegistry($client);
    $post = $registry->posts()->get(123);
    $registry->posts()->delete($post);

有关更详细的用法示例和可用方法,请参阅 文档

贡献

欢迎贡献!如果您发现错误,有改进建议,或想添加新功能,请提交问题或拉取请求。请确保遵循我们的 贡献指南

许可证

WPRestClient 是在 MIT 许可证 下许可的开源软件

联系

如有任何问题或查询,请联系 WPRestClient@gmail.com

支持

有关错误和功能请求,请使用此存储库的 问题 部分。