Sending Multiple REST API Requests using PHP Requests Library

Recently I got a message from one of my customer and he complained about that inventory sync in my Simple Inventory Sync plugin doesn’t work as fast as he wanted it to.

When I took a look at his website configuration, I noticed that he wanted to syncronise the product stock up to 10 websites at the same time.

At that moment my plugin was already using batch API requests in case someone made an order with 50 products (for example) at the same time (so we can send only a one request instead of 50), but we can not send a single batch request to 10 different sites, can we?

Even if we unload everything to WordPress Cron, 10 HTTP requests will be noticable anyway.

When I started to dive deep into and to look for a solution, I noticed, that WordPress already has a very nice PHP library included – Requests.

Then I decided to make a quick test – to send 3 REST API requests to 3 different sites (9 requests in total) and to measure the performance. And here is what I discovered:

wp_remote_request()Requests::request_multiple() 
9325.8ms1773.4ms
8769.4ms1345.6ms
9097.0ms1144.5ms
8783.5ms1121.4ms
8438.3ms1137.3ms

As you can see, it is not just faster, but incredibly faster!

I was doing these measurements by sending a simple product update request to /wc/v3/products/{product_id} where I change a product stock quantity.

With wp_remote_request() it looked that way:

$requests = array(
	// Request 1
	array(
		'url'  => "{$url}/wp-json/wc/v3/products/{$product_id}",
		'headers' => array(
			'Authorization' => 'Basic ' . base64_encode( "{$username}:{$pwd}" ),
		),
		'data' => array( 
			'stock_quantity' => 5, 
		),
	),
	// Request 2
	array(
		...
	),
	// Request 3 etc
	array(
		...
	),
);

foreach( $requests as $request ) {
	wp_remote_request(
		$request[ 'url' ],
		array(
			'method' => 'PUT',
			'headers' => $request[ 'headers' ],
			'body' => $request[ 'data' ],
		)
	);
}

Of course we need to use batch requests whenever possible but as you remember we’re sending requests to different WooCommerce stores.

Requests::request_multiple()-way:

use WpOrg\Requests\Requests;

$requests = array(
	// Request 1
	array(
		'url'  => "{$url}/wp-json/wc/v3/products/{$product_id}",
		'headers' => array(
			'Authorization' => 'Basic ' . base64_encode( "{$username}:{$pwd}" ),
		),
		'data' => array( 
			'stock_quantity' => 5, 
		),
		'type' => 'PUT',
	),
	// Request 2
	array(
		...
	),
	// Request 3 etc
	array(
		...
	),
);

Requests::request_multiple( $requests );

Do not forget to include use WpOrg\Requests\Requests; at the beginning, otherwise you will get an error “PHP Deprecated: The PSR-0 Requests_… class names in the Requests library are deprecated. Switch to the PSR-4 WpOrg\Requests\… class names at your earliest convenience.”

What do you think guys? Are you already using PHP Requests library on your projects?

Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X