monitorbacklinks / yii2-wordpress
通过 XML-RPC API 集成 WordPress CMS 的 Yii2 组件
Requires
- php: >=5.4.0
- ext-xmlrpc: *
- hieu-le/wordpress-xmlrpc-client: ~2.0
- yiisoft/yii2: *
Requires (Dev)
- codeception/codeception: ~2.0
This package is not auto-updated.
Last update: 2024-09-23 06:14:17 UTC
README
此仓库不再维护。问题报告和拉取请求将不予处理。如果您愿意,可以克隆此仓库。
Yii2 Wordpress
Yii2 Wordpress 是一个用于通过 XML-RPC API 集成 WordPress CMS 的 Yii2 框架 组件。
此组件建立在 Wordpress XML-RPC PHP 客户端 之上,由 Hieu Le Trung 开发。
要求
- Yii 2.0
- PHP 5.4
- PHP 扩展 XML-RPC
安装
通过 Composer 安装此扩展是首选方式。
运行以下命令
php composer.phar require monitorbacklinks/yii2-wordpress "dev-master"
或者在您的 composer.json
文件的 require
部分添加以下内容
"monitorbacklinks/yii2-wordpress": "dev-master"
使用
组件创建
为了使用此扩展,首先需要创建一个名为 blog
(您可以更改名称)的组件并对其进行配置。以下是最小配置示例(在您的 config/main.php
中)
'components' => [ ... 'blog' => [ 'class' => '\monitorbacklinks\yii2wp\Wordpress', 'endpoint' => 'http://example.com/xmlrpc.php', 'username' => 'demo', 'password' => 'demo' ] ... ]
首次 API 请求
组件配置后,您可以开始向您的 WordPress 网站发送请求。
例如,获取十篇最新发布的文章。仅选择 guid
、post_title
和 post_content
字段
$blogPosts = Yii::$app->blog->getPosts([ 'post_status' => 'publish', 'number' => 10 ], ['guid', 'post_title', 'post_content']);
或创建一篇标题为 "新文章" 且内容为 "Hello world!" 的新文章
$postID = Yii::$app->blog->newPost('New post', 'Hello world!');
缓存请求结果
调用外部应用程序的 API 意味着会有延迟。如果您不希望用户每次都等待 WordPress 的响应,缓存就是正确的方法
// The user profile will be fetched from cache if available. // If not, the query will be made against XML-RPC API and cached for use next time. $profile = Yii::$app->blog->cache(function (Wordpress $blog) { return $blog->getProfile(); });
如果需要更复杂的功能,可以禁用某些请求的缓存
$blogPosts = Yii::$app->blog->cache(function (Wordpress $blog) { // ... queries that use query cache ... return $blog->noCache(function (Wordpress $blog) { // this query will not use query cache return $blog->getPosts(); }); });
缓存仅适用于数据检索查询。创建、更新或删除记录的查询不会使用缓存组件。
配置参数
$endpoint
string
WordPress XML-RPC API 端点 URL。
$username
string
WordPress 认证用户名。
请注意,任何通过 XML-RPC 执行的操作都将代表此用户执行。
$password
string
WordPress 认证密码。
$proxyConfig
array
代理服务器配置。
此配置数组应遵循以下格式
proxy_ip
: 代理服务器的 IP(不包含端口)proxy_port
: 代理服务器的端口proxy_user
: 代理授权的用户名proxy_pass
: 代理授权的密码proxy_mode
: CURLOPT_PROXYAUTH 选项的值(默认为 CURLAUTH_BASIC)
空数组表示不使用代理。
默认值: []
。
$authConfig
array
服务器 HTTP 认证配置。
此配置数组应遵循以下格式
auth_user
: 服务器认证的用户名auth_pass
: 服务器认证的密码auth_mode
:CURLOPT_HTTPAUTH选项的值(默认为CURLAUTH_BASIC)
空数组表示不使用HTTP认证。
默认值: []
。
$catchExceptions
布尔值
是否捕获Wordpress API抛出的异常,将它们传递到日志并返回默认值,或进一步沿调用链传递。
默认值:true
。
$enableQueryCache
布尔值
是否启用查询缓存。
默认值:true
。
$queryCacheDuration
整数
查询结果在缓存中有效的时间(以秒为单位)。
使用0表示缓存数据永远不会过期。
默认值:3600
。
$queryCache
缓存|string
用于查询缓存的缓存对象或缓存应用程序组件的ID。
默认值:'cache'
。
可用方法列表
所有可用方法的全列表可以在Wordpress XML-RPC PHP客户端类参考中找到。
请注意,所有这些方法在出现任何错误时都会抛出异常。虽然此扩展默认配置为在出现错误时为任何数据重试方法返回空数组,为任何创建、更新或删除方法返回false。请参阅$catchExceptions
配置选项的详细信息。
错误日志
有很多事情可能会出错(网络问题、错误的Wordpress用户权限等)。如果$catchExceptions
配置选项设置为true
(默认值),则此扩展将捕获这些错误并将它们传递到monitorbacklinks\yii2wp\Wordpress::*
日志类别。
要查看它们,您可以配置您的Yii2 log
组件,类似于以下内容
'components' => [ ... 'log' => [ 'targets' => [ 'file' => [ 'class' => 'yii\log\FileTarget', 'levels' => ['error'], 'categories' => ['monitorbacklinks\yii2wp\Wordpress::*'], ], ], ], ... ]
报告
- 在GitHub上报告任何问题。
许可证
yii2-wordpress遵循MIT许可证发布。有关详细信息,请参阅捆绑的LICENSE.md
文件。