One thing that makes PHP operations simple is serialization. Each line of code runs sequentially. This is also a limitation when you need to build a program that interacts with multiple APIs or other services, because HTTP requests take time and may prevent page rendering. This article discusses this problem by using PHP to combine data from multiple rest API endpoints. The best way to avoid this problem is to use caching or JavaScript. One of the benefits of JavaScript is that there are many simple ways to execute asynchronous HTTP requests, such as the simple API of jQuery Ajax. The
PHP can be serial, but PHP applications can perform asynchronous HTTP requests in a variety of ways. I have written about WordPress asynchronous tasks before. This is a good use case for waiting for the current session to end before the task runs. Delicious brance has excellent articles on other methods of WordPress asynchronous PHP methods. However, if you need the results of multiple HTTP requests before completing the current session, these policies will not work. Instead, it is better to create multiple HTTP requests at once and continue. This article describes how to do this using word press and provides the resources needed to further use asynchronous PHP. The
When requesting WordPress 4.5 or later, the PHP library requests will be used for HTTP requests. This is very transparent, but when using functions such as wp\u remote\u get() or wp\u remote\u post(), the request is used by default. Therefore, WordPress can safely use requests by itself. A practical and simple library maintained by Ryan McCue. The default requests class contains static methods for each HTTP method. Therefore, you can use requests::get() for get requests and requests::post() for post requests. Here are two simple examples: The first is to get the post page from the remote site through the WordPress rest API. The second is to create a post on a remote site through the WordPress rest API and request post. The
Male New requests_auth_basic ([\
],
The[
\
\
]The
); It’s cool, but there’s nothing wpuremote_* () type functions can’t do. In fact, you’d better abide by this standard. But the point of this article is to discuss asynchronous requests. The
Although asynchrony is not commonly used, WordPress has the ability to execute asynchronous PHP requests from version 4.5 without additional dependencies. The requests class has a request_multiple() method. For example, to use the example of starting merging posts in the rest API of multiple sites, you can merge two get requests, as follows: Male ‘ https:\/\/calderaforms.com\/wp-json\/wp\/v2\/posts ‘,
Type = > Import
];
$request_2 =[
‘url’= >’ https:\/\/joshpress.net\/wp-json\/wp\/v2\/posts ‘,
Type = > Import
];
$response= request: request_multiple ([$request_1, $request_2]);
\/**@var requests_response $response*\/
Foreach (as $response$response){