PHP: will cURL end in the background or block all subsequent script execution?

I am creating an application that at some point twists the content from an external URL. Until now, it always ended pretty quickly / instantly. However, I'm not sure what will happen if the external server takes a long time to respond. Will PHP wait to execute the following code until cURL completes?

I cannot verify this because I do not know how to "simulate" a slower answer. I hope this pseudo code makes my question clear:

$ch = curl_init( $some_remote_url ); $fp = fopen( $some_local_file, 'wb' ); curl_setopt( $ch, CURLOPT_FILE, $fp ); curl_setopt( $ch, CURLOPT_HEADER, 0 ); curl_exec( $ch ); // Let say this takes 20 seconds until the other server responds curl_close( $ch ); fclose( $fp ); redirect( $some_other_url ); // Will this be executed instantly or only after 20 seconds? 

The reason I'm curious is because I don't want my user to look at the download page for 20 seconds if the remote server is slow to respond, so I probably have to move the whole process to a cron job. The user does not need the result of twisting instantly, so it does not matter for him when the process is completed.

+7
source share
2 answers

Curl blocks execution. If you want to download the file in the background (asynchronously), use either the scheduled cron task or run a command like this:

 system("wget URL &"); 
+4
source

I don't know if this will stop the script from executing, but you can create a script that directly displays the output to the remote computer, and then runs another script through AJAX to execute the cURL action you want. At the end, it will return the response, and your JS script will do the redirection or whatever you want.

Of course, this will work only for users who have JS, but just to say: everyone who does not have JS enabled in his browser cannot be viewed normally on the Internet.

0
source

All Articles