I'm using PHP/cURL to send 15 API requests at once(shipping rate quotes). Each API has its own file and I'm using include('example.php') to include the files into the main request page. However, as most of you already know, PHP goes line by line and this in turn takes ages to get all the responses back. Is there a way I can include these files and have them run simultaneously? I understand this is not really doable with PHP so I was wondering if there is another alternative in which I can maybe use another programming language along side of PHP to accomplish this.
Asked
Active
Viewed 642 times
0
-
this is an opinion based. try ruby and eventmachine. golang. erlang. – akonsu Jan 03 '14 at 16:18
-
there's curl_multi. but PHP is inherently single-threaded and the only other way you'll be able to fire off multiple requests in parallel is to execute your main script multiple times as separate instances. – Marc B Jan 03 '14 at 16:18
3 Answers
1
Its a bit of a beast to get your head around but lookup curl multi
Its a way of issuing multiple curl requests all in one go. I successfully ( eventually ) got it to issue 200 requests at a time. Basically you package up X requests, fire of the curl_multi which issues all the requests asyncronously. You then loop waiting for the status to tell you all the replys have been received and go and process them. ( Very basic description)

RiggsFolly
- 93,638
- 21
- 103
- 149
0
Spawn 15 threads. The easiest way to accomplish this is to start 15 requests.
You can make PHP proxy requests non-blocking by closing the connection.
To close a connection do:
ignore_user_abort(true);
header("Content-Length: 0");
header("Connection: Close");
flush();
session_write_close();
// do your API call here and report back to a database or some other means
// you can't return or output anything because the other end isn't listening any more

Halcyon
- 57,230
- 10
- 89
- 128