javascript - Asynchronous PHP - Stack Overflow

I have a php script that gets user input (some text and a link), puts it in the database and then using

I have a php script that gets user input (some text and a link), puts it in the database and then using the link the user provided, it searches for an image.

The problem is, sometimes it takes a while to search for the image, and this makes the website 'stuck', so the view that says 'Your post has been submitted!' sometimes takes between 4 to 6 seconds to finally load.

Is there a way to make PHP search for the image asynchronously?

You may be wondering why don't I just load the view and then do the image search, or just use AJAX. Well, I need the 'last insert ID' from the database to rename the image according to the item ID, and this can only be done once the post has been inserted in the database.

EDIT 1: I have already read this Asynchronous PHP calls? but I am not sure I understand.

EDIT 2: Here is an overview of the process as requested:

  • Step 1: User opens form and fills 2 fields (some text and a link)
  • Step 2: User submits form
  • Step 3: PHP inserts post in MySQL database
  • Step 4: PHP searchs for an image of that link (crawls the web, takes a few seconds) and saves the image with the name of the last ID from database insert. If last ID was 75 image will be saved as 'img_75'.
  • Step 5: PHP loads 'You have succesfully posted a link'

What I want is step 5 to happen just after step 3, while step 4 is running. I don't actually need the response from step 4. As long as it runs on the background I am happy :)


SOLUTION: I have done what @shadyyx suggests. A simple AJAX call. I really hoped for an alternative but apparently not php nor any other server side language can handle asynchronous calls.

I have a php script that gets user input (some text and a link), puts it in the database and then using the link the user provided, it searches for an image.

The problem is, sometimes it takes a while to search for the image, and this makes the website 'stuck', so the view that says 'Your post has been submitted!' sometimes takes between 4 to 6 seconds to finally load.

Is there a way to make PHP search for the image asynchronously?

You may be wondering why don't I just load the view and then do the image search, or just use AJAX. Well, I need the 'last insert ID' from the database to rename the image according to the item ID, and this can only be done once the post has been inserted in the database.

EDIT 1: I have already read this Asynchronous PHP calls? but I am not sure I understand.

EDIT 2: Here is an overview of the process as requested:

  • Step 1: User opens form and fills 2 fields (some text and a link)
  • Step 2: User submits form
  • Step 3: PHP inserts post in MySQL database
  • Step 4: PHP searchs for an image of that link (crawls the web, takes a few seconds) and saves the image with the name of the last ID from database insert. If last ID was 75 image will be saved as 'img_75'.
  • Step 5: PHP loads 'You have succesfully posted a link'

What I want is step 5 to happen just after step 3, while step 4 is running. I don't actually need the response from step 4. As long as it runs on the background I am happy :)


SOLUTION: I have done what @shadyyx suggests. A simple AJAX call. I really hoped for an alternative but apparently not php nor any other server side language can handle asynchronous calls.

Share Improve this question edited May 23, 2017 at 10:33 CommunityBot 11 silver badge asked May 22, 2012 at 13:15 John ShepardJohn Shepard 9471 gold badge9 silver badges28 bronze badges 13
  • 4 So why can't you use AJAX? Your PHP script will insert the post into the database, then you can get the last insert ID and use it in your AJAX call. You can use PHP to generate the Javascript that will make the AJAX call. – Travesty3 Commented May 22, 2012 at 13:23
  • @Travesty3 Its probably because his image-search (may be) needs to happen before the 'sumbit' create a DB record. – UltraInstinct Commented May 22, 2012 at 13:26
  • 1 I'm still not sure why you can't use AJAX. Could you add some more detail about the renaming bit in your last main paragraph, so we can see how that fits into the search? – halfer Commented May 22, 2012 at 13:26
  • @Thrustmaster: Then how would making an asynchronous call help that? – Travesty3 Commented May 22, 2012 at 13:27
  • 2 If something works by posting to the server, you can be pretty sure it'll work using ajax too as that is all ajax does... – Alex Commented May 22, 2012 at 13:27
 |  Show 8 more ments

5 Answers 5

Reset to default 4

I would do this in a two step AJAX call.

Here is what should it look like:

  1. User opens a form and fills in the data
  2. User clicks on submit button - an AJAX call is sent to the server
  3. A new entry is written into the database and last ID is returned as response back to JS
  4. You show user the message that the form was submitted
  5. and call new AJAX request that will crawl the desired URL, download and save the image with the ID You got from the first AJAX response...

Clear? Easy? I guess it is...

I used processes for this.

  1. the client requests
  2. the server answers and gives the client an answer ID
  3. the server starts a php process which does long running stuff in background and saves the result to a file
  4. the client requests the answer with the ID every few seconds
  5. when everything is in the file the server loads it and sends the result on an answer-request of the server.

used popen() for this...

also the server could tell the client a time-span which the client should wait till requesting the answer.

Why not creating a new record, returning last id immediately, and running the new query for updating the record with information?

Here is what I suggest for your case:

  • Send out a cookie to the browser when you have the form page being loaded.
  • The requests for the image should not (and must not) depend on the record that 'would be' inserted.
  • The image you are searching for is to be saved in some temporary path. Associate that path to the cookie. In PHP, you could use $_SESSION[...]. This would be an AJAX request as soon as the user fills out the required fields.
  • When form is submitted to the server:
  • Server gets the image from the $_SESSION variable.
  • PHP moves the file from the temporary location to the actual location.
  • Updates the DB.

Why you should not create a record prematurely

What would happen when AJAX request has struck the server(and the record is created) and then the user navigates away? In such cases you need to have additional constructs to remove that record, which is some sort of extra stuff you wouldn't want to get into.

I have already read this Asynchronous PHP calls?

The quality of the answers there could be better.

but I am not sure I understand

err, then maybe you might not understand the answers here.

The short answer is that you can't write asynchronous PHP code.

A longer answer is that you can make parallel HTTP requests from PHP (but still blocking) using the curl_multi_ functions, however if you want to generate a page from PHP without blocking then you'll have to generate HTML containing AJAX calls and let the browser handle the concurrent requests and populate them into the page when pleted. Using this approach you have the option of invoking mulitple search threads on your DBMS which (if done right) should run faster than a single request.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745456945a4628531.html

相关推荐

  • javascript - Asynchronous PHP - Stack Overflow

    I have a php script that gets user input (some text and a link), puts it in the database and then using

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信