drsdre / yii2-wordpress-api
Yii2 扩展,用于通过 oAuth 或基本认证连接到 WordPress Rest API
2.3
2018-02-20 15:57 UTC
Requires
- php: >=5.5.0
- yiisoft/yii2: ~2.0.14
- yiisoft/yii2-authclient: ~2.1.3
This package is auto-updated.
Last update: 2024-08-29 04:31:15 UTC
README
WordPress Rest API 的 Yii2 客户端(自 WordPress 4.7 核心部分)
完整的 API 文档在此:http://v2.wp-api.org/
要求
PHP5 带有 CURL 扩展。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
composer require --prefer-dist drsdre/yii2-wordpress-api "*"
或者
"drsdre/yii2-wordpress-api": "*"
将以下内容添加到您的 composer.json
文件的 require
部分。
授权设置
使用以下任一方式设置连接:
- oAuth1(需要 https://wordpresstheme.cn/plugins/rest-api-oauth1/)
- 基本认证(需要 https://github.com/WP-API/Basic-Auth 警告:不建议在生产环境中使用)
oAuth1
以下示例使用模型存储 WordPress 授权数据。所需字段包括
- site_url [字符串]
- client_key [字符串]
- client_secret [字符串]
- access_token [Json 字符串]
您需要一个网络动作来授权 oAuth1 访问。
/** * Execute oAuth verification * * @param $id of wordpress_site record * @param null $oauth_token * * @return yii\web\Response */ public function actionVerifyAccess($id, $oauth_token = null) { $this->findModel( $id ); // Open Wordpress Auth API $oauthClient = new WordpressAuth([ 'apiBaseUrl' => $this->site_url, // https://www.yoursite.com/ (without API directory) 'consumerKey' => $this->model->client_key, 'consumerSecret' => $this->model->client_secret, ]); try { if (is_null($oauth_token)) { // If no authorisation token, start authorization web flow // Must set return URL without parameter to prevent 'OAuth signature does not match' error $oauthClient->setReturnUrl( yii::$app->getRequest()->getHostInfo().'/'. yii::$app->getRequest()->getPathInfo().'?id='.$id); // Get request token $oauth_token = $oauthClient->fetchRequestToken(); // Get authorization URL $url = $oauthClient->buildAuthUrl($oauth_token); // Redirect to authorization URL return $this->redirect($url); } // After user returns at our site: $access_token = $oauthClient->fetchAccessToken($oauth_token); // Upgrade to access token $this->model->access_token = yii\helpers\Json::encode($access_token->params); // Save token to record $result = $this->model->save(); } catch (yii\base\Exception $e) { yii::$app->session->setFlash( 'alert', [ 'body' => yii::t( 'app', 'Verification failed. Error: ' ).$e->getMessage(), 'options' => [ 'class' => 'alert-danger' ], ] ); } // Redirect to main overview return $this->redirect('/wordpress_site/'); }
使用访问令牌,WordPress API 可以初始化如下:
$wordpress_credentials = [ 'endpoint' => $WordpressSite->site_url, 'client_key' => $WordpressSite->client_key, 'client_secret' => $WordpressSite->client_secret, 'access_token' => Json::decode( $WordpressSite->access_token ), ]; $WordpressApiClient = new drsdre\WordpressApi\Client( $wordpress_credentials );
基本认证
$wordpress_credentials = [ 'endpoint' => $WordpressSite->site_url, 'username' => $WordpressSite->username, 'password' => $WordpressSite->password ]; $WordpressApiClient = new drsdre\WordpressApi\Client( $wordpress_credentials );
使用 API
一旦 WordPress API 客户端获得授权,就可以向 API 发送请求。
检索分页数据
$api_post_page = 1; do { $ApiResult = $WordpressApiClient->getData( '', 'edit', $api_post_page, $WebsiteWp->get_page_size ); $data = $ApiResult->asArray(); < do something with the data > } while ( $api_post_page <= $ApiResult->result_total_pages );
查看