metaline / wp-api-client
A PHP client for the WordPress REST API, based on Guzzle.
Requires
- php: ^7.4 || ^8.0
- ext-json: *
- guzzlehttp/guzzle: ^7.4
- psr/log: ^1.0 || ^2.0 || ^3.0
Requires (Dev)
- jeroen/psr-log-test-doubles: ^2.2 || ^3.2
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2025-01-25 09:00:26 UTC
README
A PHP client for the WordPress REST API, based on Guzzle.
Installation
The recommended way to install this library is through Composer.
composer require metaline/wp-api-client
Documentation
Create the client
You can create the client instance through the ClientFactory
. At the moment, only WooCommerce credentials are supported:
use MetaLine\WordPressAPIClient\ClientFactory; $factory = new ClientFactory(); $client = $factory->createFromWooCommerceCredentials( $customerKey, $customerSecret, $apiUrl );
If you need to access to the WordPress REST API through the WooCommerce API credentials, you need this hook in YOUR installation of WordPress:
add_filter('woocommerce_rest_is_request_to_rest_api', function ($enabled) { if (!$enabled) { $rest_prefix = trailingslashit(rest_get_url_prefix()); $request_uri = esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])); $enabled = false !== strpos($request_uri, $rest_prefix . 'wp/'); } return $enabled; });
Fetch data from REST API
Through the client instance you can fetch data from the WordPress REST API. For example:
$customers = $client->request('GET', 'wc/v3/customers');
Please, refer to the WordPress and WooCommerce REST API documentation, for all available methods.
Helper methods
The client has five helper methods, one for each REST verb: get()
, post()
, put()
, patch()
and delete()
.
$uri
is the endpoint of the call;$query
is an array of variables to put in query string;$params
is an array of data to put in the body request;
Upload files
A special case is the media endpoints, which allow us to upload a file:
$data = [ 'file' => new SplFileObject('/path/to/file.zip'), ]; $client->post('wp/v2/media', $data);
License
This project is made available under the MIT License (MIT). Please see License File for more information.