Vinayak Phal
Vinayak Phal

Reputation: 8919

PHP How to implement multiple web services simultaniously?

I'm working on an energy project. In my site there are may energy services are registered with there enrollment process through web services. Initially it was 1 to begin with now 3 more came to integrate.

On the very first page I've to show the plans based on the zip codes. Now on this page I've to request to all 4 energy provider to list the energy plans.

To request each service at a time in sequential way definitely it will take more time as I've to get the data from external source and this is the first page so I do not want to make it slow down at the first instance only.

How can I make request to all the services simultaneously and collect the data from each services and list it on the final page.

Upvotes: 5

Views: 597

Answers (1)

pestilence669
pestilence669

Reputation: 5701

There is really only one non-nasty way to do this with PHP when executing on a web server. You will need the cURL module compiled into PHP. It has built-in support for executing parallel requests. Look at the comments and documentation on www.php.net for usage examples.

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

Other options, like forking background processes or calling a UNIX shell, are terrible to maintain and rarely cross-platform. Multithreading isn't a valid option in nearly any common PHP environment.

I should mention, although not necessarily suggest, that an AJAX approach could be used. It's not as lightweight in all cases, but it should be weighed.

Upvotes: 3

Related Questions