metaline/wp-api-client

A PHP client for the WordPress REST API, based on Guzzle.

Maintainers

Package info

github.com/metaline/wp-api-client

pkg:composer/metaline/wp-api-client

Transparency log

Statistics

Installs: 29

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 7

0.1.0 2026-08-05 15:34 UTC

This package is auto-updated.

Last update: 2026-08-05 15:40:34 UTC


README

Test library

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

The factory accepts an optional array of options, to customize the timeouts of the requests:

$client = $factory->createFromWooCommerceCredentials(
    $customerKey,
    $customerSecret,
    $apiUrl,
    [
        'timeout'         => 60,
        'connect_timeout' => 5,
    ]
);
Option Default Description
timeout 120 Maximum number of seconds to wait for the whole request.
connect_timeout 10 Maximum number of seconds to wait while trying to connect.

Both options are passed to Guzzle, so you can refer to the Guzzle documentation for more details.

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().

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;

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

Retries

A request is sent again, up to 5 times in total, when it fails at the transport level — the host cannot be resolved, the connection is refused or the server times out — and when the server answers with 500, 502, 503 or 504. The other server errors, such as 501 and 505, describe a server that will keep answering the same way, so they are not retried.

Only the methods that are idempotent by definition are retried: GET, HEAD, OPTIONS, TRACE, PUT and DELETE. A POST or a PATCH is sent once and never repeated, because neither a transport error nor a 500 tells us whether the server has already processed the request: a timeout on POST wc/v3/orders, or a fatal error raised by a hook after the order has been saved, may well leave an order behind, and retrying would create a second one.

Retries are sent one after the other, with no delay in between.

License

This project is made available under the MIT License (MIT). Please see License File for more information.