A PHP client for the WordPress REST API, based on Guzzle.
The recommended way to install this library is through Composer.
composer require metaline/wp-api-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;
});
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.
The client has five helper methods, one for each REST verb: get()
, post()
, put()
, patch()
and delete()
.
HTTP verb | Helper method |
---|---|
GET | $client->get($uri, $query) |
POST | $client->post($uri, $params) |
PUT | $client->put($uri, $params) |
PATCH | $client->patch($uri, $params) |
DELETE | $client->delete($uri, $query) |
$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;
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);
This project is made available under the MIT License (MIT). Please see License File for more information.