PHP How to implement multiple web services at the same time?

I am working on an energy project. You can register energy services on my website using the registration process through the web services. Initially, 1 was launched, and now 3 more people have integrated.

On the first page I have to show plans based on zip codes. Now on this page I have to ask all 4 energy providers to list their energy plans.

To request each service at a time in sequential order, it will take longer because I have to get the data from an external source, and this is the first page, so I do not want it to slow down in the first instance only.

How can I make a request to all services at the same time and collect data from each service and list them on the last page.

+5
source share
1 answer

In fact, there is only one nasty way to do this with PHP when executed on a web server. You will need the cURL module compiled in PHP. It has built-in support for running parallel queries. See comments and documentation at www.php.net for use.

Try: http://www.php.net/manual/en/function.curl-multi-init.php

Other options, such as formatting background processes or invoking a UNIX shell, are terrible to maintain and rarely cross-platform. Multithreading is not a valid option in almost any common PHP environment.

I should mention, although not necessarily suggest, that you can use the AJAX approach. It is not so easy, but you need to weigh it.

+3
source

All Articles