Using Wordpress Rest Api with Laravel Guzzle

nowaday , rest api is popular among mobile developer and web developer because it easy to use , you can use rest api to do any action like control wordpress from we start our lession with wordpress version 4.8 and laravel 5.4.*.

step 1 : start to install plugin if you not yet install on wordpress

  • WP BASIC Auth
  • JWT Authentication for WP-API

follow instruction in plugins description carefully, you won't get errors. if you install plugins successfully you can access these urlfrom soap-ui (you can use chrome's Apps rest advance client) :

http://localhost/wordpress/wp-json/jwt-auth/v1/token?username=wordpress_user&password=wordpress_password (jwt token link) and what you will get like these

"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3RcL3dvcmRwcmVzcyIsImlhdCI6MTUwMDgwNDU0OSwibmJmIjoxNTAwODA0NTQ5LCJleHAiOjE1MDE0MDkzNDksImRhdGEiOnsidXNlciI6eyJpZCI6IjEifX19.tlj_LbJTgvI31lGYWnUrIZ_UVyN12U3kRAbu4ppKZjw",
"user_email": "[email protected]",
"user_nicename": "admin",
"user_display_name": "admin"

http://localhost/wordpress/wp-json/wp/v2/posts (this link where you gonna post your data) :

//request header
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3RcL3dvcmRwcmVzcyIsImlhdCI6MTUwMDgwNDU0OSwibmJmIjoxNTAwODA0NTQ5LCJleHAiOjE1MDE0MDkzNDksImRhdGEiOnsidXNlciI6eyJpZCI6IjEifX19.tlj_LbJTgvI31lGYWnUrIZ_UVyN12U3kRAbu4ppKZjw[

//request as option method
//respond data
"namespace": "wp/v2",
"methods": [
"endpoints": [
"methods": [
"args": {
"context": {
"required": false,
"default": "view",
"enum": [
"description": "Scope under which the request is made; determines fields present in response.",
"type": "string"
"page": {



step 2 : start our client side. we install rest client guzzle .it good to use with framework like laravel

//start your project
laravel new wordpressclient

//install guzzle
composer require guzzlehttp/guzzle


step 3 : time to code, create new controller

php artisan make:controller wpclient

then it look like this


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use GuzzleHttp\Client as client;

class wpclient extends Controller
	public function postfunction(){
		$user = 'admin';
	    $pass = '123456';

	    $client = new client(['base_url' => 'http://localhost/wordpress/wp-json/jwt-auth/v1/']);

	    //according to plugin we must use POST method
	    $session = json_decode($client->request('POST','token?username='.$user.'&password='.$password)->getBody()->getContents());

	    // after grantd token we start post data to wordpress

	    $postclient = new client(['base_url' => 'http://localhost/wordpress/wp-json/wp/v2']);

	    $respond = $postclient->request('POST','posts',[
	    	'headers' =>[
	    		'Accept' => 'application/x-www-form-urlencoded',// this for case error not authorized, normally guzzle handle the rest for you
	    		'Authorization' => 'Bearer '.$session->token
	    	// this is where you post data
	    	'form_params' => ['title' => 'your_title','excerpt' => 'your_except','status' => 'publish','content' => 'your_content']

	    return $respond->getBody();


step 4 : route for use our client

Route::get('/post','[email protected]');


done let test . for UPDATE , DELETE , READ  you can check out from

Share this Post:

Related Posts: