Running PHP script in the background without curls

I have a little problem running php script in background.

I have my administration area that has HTTP authentication for access and a mail script that I want to run in the background. This script will take a lot of time, so I want it to run in the background.

My idea is that when I go to the Submit page, it sends the script in the background and redirects the user to another place.

However, we are currently trying to use cURL, I cannot cURL in the send script since it returns the required authorization.

Any advice is appreciated, thanks.

+3
source share
7 answers
  • ( // ).
  • deamon () cron, .
  • , , ( // ..), ( ).
+4

pcntl_fork(), Linux. , PHP (pcntl cgi) , script Apache. . PHP, .

, :

if ($pid = pcntl_fork())
    die();     // Parent

function shutdown() {
    posix_kill(posix_getpid(), SIGHUP);
}

ob_end_clean(); // Discard the output buffer and close

fclose(STDIN);  // Close all of the standard
fclose(STDOUT); // file descriptors as we
fclose(STDERR); // are running as a daemon.

register_shutdown_function('shutdown');

if (posix_setsid() < 0)
    die();      // <- This is an error

if ($pid = pcntl_fork())
    die();     // Parent

// Do your stuff here
+1

cURL HTTP...

HTTP-... , PHP ...

/** 
 * Asynchronously execute/include a PHP file. Does not record the output of the file anywhere.  
 * Relies on the PHP_PATH config constant.
 *
 * @param string $filename  file to execute
 * @param string $options   (optional) arguments to pass to file via the command line
 */ 
function asyncInclude($filename, $options = '') {
    exec(PHP_PATH . " -f {$filename} {$options} >> /dev/null &");
}

( PHP_PATH , define('PHP_PATH', '/opt/bin/php5') )

. PHP, . argv.

+1

auth cURL?

, http://user:pass@www.domain.com

- :

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

FWIW , cURL - , , , - auth

0

, PHP script, PHP . CURL, PHP .

0

1:

system('php your_script &'); - .

2:

script, cURL.

0

- - , , , , , 10 /, 20 /. Ours Cron ( , ). - script, , , - . , . - , .

If your letters will approach 15K / batch, I would recommend checking the hosted SMTP solution: http://smtp.com or http://www.socketlabs.com/ Queue through your server and send via SMTP relay to ensure reliable delivery .

No matter how many letters you send, do not forget about the correspondence of CAN spam!

0
source

Source: https://habr.com/ru/post/1755321/


All Articles