I am working on an a PHP web application and i need to perform some network operations in the request like fetching someone from remote server based on user's request.
Is it possible to simulate asynchronous behavior in PHP given that i have to pass some data to a function and also need output from it.
My code is like:
<?php
$data1 = processGETandPOST();
$data2 = processGETandPOST();
$data3 = processGETandPOST();
$response1 = makeNetworkCall($data1);
$response2 = makeNetworkCall($data2);
$response3 = makeNetworkCall($data3);
processNetworkResponse($response1);
processNetworkResponse($response2);
processNetworkResponse($response3);
/*HTML and OTHER UI STUFF HERE*/
exit;
?>
Each network operation takes around 5 seconds to complete adding a total of 15 seconds to the response time of my application given i make 3 requests.
The makeNetworkCall() function just do a HTTP POST request.
The remote server is an 3rd party API so i don't have any control over there.
PS: Please do not answer giving suggestions about AJAX or Other things. I am currently looking if i can do this through PHP may be with an C++ extension or something like that.
What Is Asynchronous PHP? Asynchronous PHP refers to PHP code that is written using the asynchronous model. In other words, asynchronous applications can multi-task. This is critical because traditionally, there's a lot of time when a CPU sits idle while a PHP application manages I/O tasks.
Guzzle 6: Guzzle is a PHP HTTP client helps to send the HTTP requests. These methods can be used to send the asynchronous HTTP requests.
php /* * Executes a PHP page asynchronously so the current page does not have to wait for it to finish running. * */ function post_async($url, array $params) { foreach ($params as $key => &$val) { if (is_array($val)) $val = implode(',', $val); $post_params[] = $key. '='.
If an API call is synchronous, it means that code execution will block (or wait) for the API call to return before continuing. This means that until a response is returned by the API, your application will not execute any further, which could be perceived by the user as latency or performance lag in your app.
Nowadays, it's better to use queues than threads (for those who don't use Laravel there are tons of other implementations out there like this).
The basic idea is, your original PHP script puts tasks or jobs into a queue. Then you have queue job workers running elsewhere, taking jobs out of the queue and starts processing them independently of the original PHP.
The advantages are:
I dont have a direct answer, but you might want to look into these things:
job
is done, insert a new job describing the work that needs to be done in order to process the cached HTTP response body.This old question has a new answer. There are now a few "async" solutions for PHP these days (which are equivalent to Python's multiprocess in the sense that they spawn new independent PHP processes rather than manage it at the framework level)
The two solutions I have seen are
Give them a try!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With