PHP curl timing mismatch

Posted by JonoB on Stack Overflow See other posts from Stack Overflow or by JonoB
Published on 2010-06-01T18:30:13Z Indexed on 2010/06/01 18:33 UTC
Read the original article Hit count: 248

Filed under:
|

I am running a php script that:

  1. queries a local database to retrieve an amount
  2. executes a curl statement to update an external database with the above amount + x
  3. queries the local database again to insert a new row reflecting that the curl statement has been executed.

One of the problems that I am having is that the curl statement takes 2-4 seconds to execute, so I have two different users from the same company running the same script at the same time, the execution time of the curl command can cause a mismatch in what should be updated in the external database. This is the because the curl statement has not yet returned from the first user...so the second user is working off incorrect figures.

I am not sure of the best options here, but basically I need to prevent two or more curl statements being run at the same time.

I thought of storing a value in the database that indicates that the curl statement is being executed at that time, and prevent any other curl statements being run until its completed. Once the first curl statement has been executed, then the database flag is updated and the next one can run. If this field is 'locked', then I could loop through the code and sleep for (5) seconds, and then check again if the flag has been reset. If after (3) loops, then reset the flag automatically (i've never seen the curl take longer than 5 seconds) and continue processing.

Are there any other (more elegant) ways of approaching this?

© Stack Overflow or respective owner

Related posts about php

Related posts about curl