Rich Bradshaw
Rich Bradshaw

Reputation: 72975

How to write async PHP code

I have some code that does something like this pseudocode:

Use CURL to get site1 into a variable
Use CURL to get site2 into a variable
Use CURL to get site3 into a variable
Use CURL to get site4 into a variable

Do lots of processing on each

Produce output

echo output

Currently this takes a long time occasionally.

I'd like to get the CURL bit to run in parallel so I can get things to speed up.

How do I do that?

Upvotes: 3

Views: 1360

Answers (3)

Rich Bradshaw
Rich Bradshaw

Reputation: 72975

Just for future people, I'm actually planning to move some of the logic to the client, then use AJAX to request the PHP files with one CURL in each file. This way I can request all files at once, then use JS to parse and tidy up the results.

Upvotes: 0

Imran Naqvi
Imran Naqvi

Reputation: 2232

I think you are looking for this http://php.net/manual/en/function.curl-multi-init.php parallel cURL requests.

Upvotes: 2

Jakob Alexander Eichler
Jakob Alexander Eichler

Reputation: 3056

As far as I know php does not support asynchronous IO.

But I can recommend on Node.js,which does support it and that's it's advantage. An additional advantage of Node.js is that you can use your form validations on client and on server side.

Upvotes: 0

Related Questions