1. Go to this page and download the library: Download drsdre/yii2-wordpress-api library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
drsdre / yii2-wordpress-api example snippets
/**
* 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/');
}
$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 );
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.