I am making a PHP script that sends a file to another server to process, the process takes 5-8 secs. Should I use CURL to upload and wait for reply, or use AJAX call to check every second for update?


I am making a PHP script that sends a file to another server to process, the process of the file takes 5 to 8 seconds. Should I use CURL to upload and wait for reply, or upload it and use AJAX call to check every second for an update?

I am not sure which approach is better for the servers performances, we send a lot of files every second. I am afraid at some point it gets congested.

Any ideas/tips?

Thank you.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Ray PaseurConnect With a Mentor Commented:
You can go two different ways with CURL.  You can do a GET request, which is synchronous.  Your CURL-GET script can wait until the process is finished and the remote server responds, or it can timeout (There are lots of CURL options).  You can do a POST request which is asynchronous.  Your CURL-POST script can start the process on the remote server and then keep right on doing other things while the remote server works.  This is similar to starting a remote script with fSockOpen().

Not sure if this would be useful to you because I do not know enough about the nature of the application or the required client experience, but I will suggest it anyway.  Let's call our servers "home" and "away."  Each server can have asynchronous processes running.  When the home server wants to make a request to the away server, it will log the request in a data base, and use a RESTful interface to call the away server.  The home server will pass the data base key along with the request.  The away server will process the request and when it is complete it will make a RESTful request to the home server, returning the response and the data base key.  When the home server receives the response, it can use the returned key to go into the data base and mark the request complete.  This kind of design will give you the maximum independence between the servers and allow for load balancing.
Ray PaseurCommented:
Why does the other server take so long to process the file?  Where is the bottleneck?
I would use ajax, but I wouldn't check every second for a response, I would just use a callback with a reasonable timeout. While it's processing, just display one of those spinny animated gifs so that it looks like it's doing something.

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

OctalysAuthor Commented:

Ray_Paseur, we have to analyse the file and do something with it. Sadly the process takes about 5 secs, its not something we will improve soon.

We will using extra servers for analysing to balance the load. But what I am wondering is, what is the best approach for the web servers. Wait with after CURL or AJAX calls? Or perhaps something else?
OctalysAuthor Commented:
We did use AJAX calls before with another different project that does the same, but the process took like 5 seconds to a few minutes to complete.

But we didnt have to send files every second, so it wasnt that bad. But once awhile due external problem. It gets too congested and the AJAX calls were killing the servers!
OctalysAuthor Commented:
Oyeah and the files and user submitted, so we cant really stop the process of incoming files.
OctalysAuthor Commented:
I have been thinking myself after posting this question, and my conclusion is the same as you suggested.

Thank you for your answers!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.