Search code examples
phpmysqlajaxcurlphpmailer

Execute script in background


I have a slight issue with my code which I have been trying to handle. I am trying to send mail using php at different times in my project and most of them are user triggered. Since there are different mailing points and pages, I created a single mailing script where I send all the mailing requests to (that way, all emails are sent from a single page).

For instance, after signup, a verification code from the signup script is sent to the mailing script which sends a verification email to the user. Then the signup script waits for a response from the mailing script before it redirects to the next page. But because of a lot of factors, the mailing script takes sometime to load.

I don't want the user experiencing this kind of delay and so I have been looking for a way to send the data to the mailing script in the background while the user is redirected to another page instead of waiting for a response from the mailing script.

I have tried to use AJAX to send the data directly from the front-end but I use a validation process where certain values are authenticated from the database and so I have to send from the signup script. I have been looking for php equivalent for AJAX where I don't have to wait for a response from the mailing script before the user is redirected. I have tried CURL but somehow, it stills waits for response.

This is my cURL code:

$url = "https://requiredurl";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
$data = curl_exec($ch);
curl_close($ch);
header("location: returnurl");      
exit();

I want it to redirect to the redirect url immediately after the cURL command is executed without waiting for response.

I need help with any other php method with which I can get this to work. Thanks in advance.


Solution

  • If you're indeed collecting user information via a signup script, I think it is not really safe to use a GET method. You should rather use the POST method:

    $postData = array(
        "data1" => "Test Data1",
        "data2" => "Test Data2",
        "data3" => "Test Data3"
    );
    
    $url = "https://requiredurl";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_exec($ch);
    

    But if you decide to go on, something that might work for you is to remove curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); or you can easily comment it out. This way, the command runs, but cURL doesn't send back or display any response.

    This is not advisable as you won't know if the command ran or not but it might solve your problem at least.

    Another Method

    Will be to add this code:

    // don't let user kill the script by hitting the stop button
    ignore_user_abort(true);
    
    // don't let the script time out
    set_time_limit(0);
    
    // start output buffering
    ob_start();  
    
    // If you need to return data to the browser, run that code
    // here. For example, you can process the credit card and
    // then tell the user that their account has been approved. 
    
    usleep(1500000); // delay before redirecting user
    
    // now force PHP to output to the browser...
    $size = ob_get_length();
    header("Content-Length: $size");
    header('Connection: close');
    ob_end_flush();
    ob_flush();
    flush(); 
    // everything after this will be executed in the background.
    // the user can leave the page, hit the stop button, whatever.
    usleep(2000000); // delay before executing background code
    

    Before the mail function line.

    This way, the code will be executed in the background while the user is redirected to another page.

    I hope this helps