StuyvesantBlue
StuyvesantBlue

Reputation: 145

Run PHP + Curl in background after user abort

I've got an online store and I make use of an online accounting software where I manually post orders to. The online accounting software has a very big api and I would like to send orders over automatically when a customer places an order.

Once an order is completed the customer lands up on the Success Page, i.e. successpage.php

In this page I've got the following:

$sendOrder = file_get_contents("https://myonlinestore.com/sendorder.php?order=1234");

On sendorder.php, I receive the $_GET parameter "order" which is the order number, and the I process several SQL requests to retrieve data of the order from the database.

Once I've got all this data, I then initiate a CURL post to send the data using the API of the accounting system.

Here is a watered-down version of my code that contains the essential parts:

$orderNum = htmlspecialchars($_GET["order"]) // SENT OVER FILE_GET_CONTENTS

// bOf process SQL here and get order info stored in various variables
// EXECUTE SQL HERE
// eOf process SQL here and get order info stored in various variables

$invoice = array(
'customer_id' => $custaccount,
'estimate_number' => $orderRef,
'reference_number' => $orderNum
// MANY OTHER VARIABLES ENTERED HERE, BUT LEFT OUT TO KEEP THINGS SHORT
);

$jsonInvoice = json_encode($invoice);


$url = 'https://ACCOUTINGAPP.com/api/v2/orders';

$data = array(
'authtoken'  => '***********',
'JSONString'  => $jsonInvoice,
'company_id' => '***********'     

);


$ch = curl_init($url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded") );

$response = false;

$response = curl_exec($ch);
curl_close($ch);


// TEST RESPONSE

if($response !== false) {

var_dump($response);
}
else
{
echo "oops error hehehe";
}

MY MAIN CONCERN:

I expect the user to immediately close the tab or page once they're on successpage.php.

But I would like to ensure that the successpage.php's $sendOrder = file_get_contents() and the code that it executes on sendorder.php continues running regardless of user connection.

So my question is, where would I put:

ignore_user_abort(TRUE);

Also, should I use output buffering? I'm only asking because I read a post about this on some other website and it advised this.

And lastly, should I include:

set_time_limit(0);

Upvotes: 1

Views: 241

Answers (1)

Marcin Orlowski
Marcin Orlowski

Reputation: 75645

Call ignore_user_abort(TRUE); as soon as you can. And you do not need output buffering as noone is going to see your output once browser tab is closed anywyay, so you just need to ensure your script continues if it was already doing anything.

Upvotes: 1

Related Questions