2017 © Pedro Peláez
 

yii2-extension yii2-wordpress-api

Yii2 Extension to connect with a Wordpress Rest API using oAuth or basic authentication

image

drsdre/yii2-wordpress-api

Yii2 Extension to connect with a Wordpress Rest API using oAuth or basic authentication

  • Tuesday, February 20, 2018
  • by drsdre
  • Repository
  • 2 Watchers
  • 5 Stars
  • 440 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 2 Open issues
  • 10 Versions
  • 3 % Grown

The README.md

Yii2 Wordpress API

Yii2 client for Wordpress Rest API (part of core as of Wordpress 4.7), (*1)

Full API Documentation here: http://v2.wp-api.org/, (*2)

Requirements:

PHP5 with CURL extensions., (*3)

Installation

The preferred way to install this extension is through composer., (*4)

Either run, (*5)

composer require --prefer-dist drsdre/yii2-wordpress-api "*"

or add, (*6)

"drsdre/yii2-wordpress-api": "*"

to the require section of your composer.json file., (*7)

Authorisation Setup

Setup a connection using either using - oAuth1 (requires https://wordpress.org/plugins/rest-api-oauth1/) - Basic Authentication (requires https://github.com/WP-API/Basic-Auth Warning: NOT recommended for production), (*8)

oAuth1

The example below uses a model to store Wordpress authorisation data. The fields required are: - site_url [string] - client_key [string] - client_secret [string] - access_token [Json string], (*9)

You need a web action to authorize the oAuth1 access., (*10)

    /**
     * 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/'); 
    }

With the access token, the Wordpress API can be initialised like this:, (*11)

$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 );

Basic Authentication

$wordpress_credentials = [ 
   'endpoint' => $WordpressSite->site_url,
   'username' => $WordpressSite->username,
   'password' => $WordpressSite->password
];

$WordpressApiClient = new drsdre\WordpressApi\Client( $wordpress_credentials );

Use the API

Once the Wordpress API Client is authorized, request can be made to the API., (*12)

Retrieve paged data:, (*13)

$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 );

See, (*14)

That's all!

The Versions

20/02 2018

dev-master

9999999-dev https://github.com/drsdre/yii2-wordpress-api

Yii2 Extension to connect with a Wordpress Rest API using oAuth or basic authentication

  Sources   Download

BSD-3-Clause

The Requires

 

by Andre Schuurman

wordpress api extension oauth rest yii2 cms

20/02 2018

2.3

2.3.0.0 https://github.com/drsdre/yii2-wordpress-api

Yii2 Extension to connect with a Wordpress Rest API using oAuth or basic authentication

  Sources   Download

BSD-3-Clause

The Requires

 

by Andre Schuurman

wordpress api extension oauth rest yii2 cms

18/12 2017

dev-add-code-of-conduct-1

dev-add-code-of-conduct-1 https://github.com/drsdre/yii2-wordpress-api

Yii2 Extension to connect with a Wordpress Rest API using oAuth or basic authentication

  Sources   Download

BSD-3-Clause

The Requires

 

by Andre Schuurman

wordpress api extension oauth rest yii2 cms

18/12 2017

2.2.1

2.2.1.0 https://github.com/drsdre/yii2-wordpress-api

Yii2 Extension to connect with a Wordpress Rest API using oAuth or basic authentication

  Sources   Download

BSD-3-Clause

The Requires

 

by Andre Schuurman

wordpress api extension oauth rest yii2 cms

02/08 2017

2.2

2.2.0.0 https://github.com/drsdre/yii2-wordpress-api

Yii2 Extension to connect with a Wordpress Rest API using oAuth or basic authentication

  Sources   Download

BSD-3-Clause

The Requires

 

by Andre Schuurman

wordpress api extension oauth rest yii2 cms

02/06 2017

2.1

2.1.0.0 https://github.com/drsdre/yii2-wordpress-api

Yii2 Extension to connect with a Wordpress Rest API using oAuth or basic authentication

  Sources   Download

BSD-3-Clause

The Requires

 

by Andre Schuurman

wordpress api extension oauth rest yii2 cms

15/02 2017

2.0.1

2.0.1.0 https://github.com/drsdre/yii2-wordpress-api

Yii2 Extension to connect with a Wordpress Rest API using oAuth or basic authentication

  Sources   Download

BSD-3-Clause

The Requires

 

by Andre Schuurman

wordpress api extension oauth rest yii2 cms

01/02 2017

2.0

2.0.0.0 https://github.com/drsdre/yii2-wordpress-api

Yii2 Extension to connect with a Wordpress Rest API using oAuth or basic authentication

  Sources   Download

BSD-3-Clause

The Requires

 

by Andre Schuurman

wordpress api extension oauth rest yii2 cms

30/01 2017

1.1

1.1.0.0 https://github.com/drsdre/yii2-wordpress-api

Yii2 Extension to connect with a Wordpress Rest API using oAuth or basic authentication

  Sources   Download

BSD-3-Clause

The Requires

 

by Andre Schuurman

wordpress api extension oauth rest yii2 cms

06/01 2017

1.0

1.0.0.0 https://github.com/drsdre/yii2-wordpress-api

Yii2 Extension to connect with a Wordpress Rest API using oAuth or basic authentication

  Sources   Download

BSD-3-Clause

The Requires

 

by Andre Schuurman

wordpress api extension oauth rest yii2 cms