Send multiple xml requests in parallel in PHP? Possible?
I have a script that makes calls to an XML api on a remote server. Currently my script sends 10 requests serially to the remote server. It fully processes each request before sending the next one. This is a huge bottleneck for my server at the moment since each API request can take up to a second each. Since most of the time is spent waiting for the remote server to respond, I'm wondering if/how I can send the requests in parallel so that all 10 requests use the same one second latency instead of ten one second latencies...
I thought about calling the script 10 times using a system command and running them in the background to ef开发者_如何学JAVAfectively create 10 processes, but I'm not sure if that's the best way to do it. I figure this problem has probably been solved before.
Yes, you can use curl. See here in the manual.
You can also use non-blocking I/O.
If you can use system command, then it is possible.
- Create php scripts what do the request and write the response data to files (10 files in total)
- Write a php script that call system command to run those 10 php script above, then wait for all executions is done
- Read the response data from files
PHP doesn't support threads, but you use curl_multi.
That will send your requests in parallel. This is a good solution because most of the time you are waiting on the network anyway.
If you design carefully (use a queue for urls, issue a processing callback when each one is done) you won't have to wait until the longest request is finished
What you're looking for is probably asynchronous calls. It can be done a couple of different ways in PHP depending on the version you're using. Some great information on it in this question: Asynchronous PHP calls?
精彩评论