A small client to request data from a wordpress project using the built-in JSON api.
This package uses kitetail/zttp which is a lightweight Guzzle client.
$ composer require jeffochoa/laravel-wordpress-client
Publish the config file:
$ php artisan vendor:publish --laravel-wordpress
Add your wordpress site API endpoint to the .env
file
WP_API_URL=
Here you have a list of the available methods:
$wordpress = new WordpressClient();
$response = $wordpress->categories();
$response = $wordpress->media();
$response = $wordpress->pages();
$response = $wordpress->posts();
$response = $wordpress->statuses();
$response = $wordpress->tags();
$response = $wordpress->taxonomies();
$response = $wordpress->types();
$response = $wordpress->users();
$response = Wordpress::categories();
All the methods receive an array to be use as part of the query
$response = Wordpress::posts(['page' => 1, 'per_page' => 1]);
Here you can get a list of the arguments that you can use on each request.
As an array:
$response = Wordpress::posts(['page' => 1, 'per_page' => 1])->json();
As a collection:
$response = Wordpress::posts(['page' => 1, 'per_page' => 1])->collection();
To run the tests you can clone this project then:
$ composer install
And finally you need to create your own phpunit.xml
file
$ cp phpunit.example phpuni.xml
Don't forget to set the wordpress api endpoint in your phpunit.xml
file
<env name="WP_API_URL" value="www.wordpress.dev/wp-json/wp/v2"/>
Pull requests and issues are welcome.
Say hi on twitter: @Jeffer_8a