Call a URL in background using PHP

0
741
Call a URL in background using PHP

To run a URL in the background using PHP, you can make use of the exec() or shell_exec() functions along with the nohup command.

Here’s an example:

<?php
$url = 'http://example.com/some_script.php';

// Build the command to run in the background
$command = 'nohup wget -qO /dev/null '.$url.' > /dev/null 2>&1 &';

// Execute the command
exec($command);
?>

In the above code, we’re using the wget command to make a request to the specified URL. The output of the command is redirected to /dev/null to discard any output, and the process is detached from the current shell using nohup. The & symbol at the end of the command ensures that it runs in the background.

Please note that running a URL in the background using exec() or shell_exec() depends on the server configuration and whether the necessary commands are available. Additionally, running processes in the background can have security implications, so make sure to validate and sanitize the URL before executing it.

ALSO READ  How to Zip/Unzip files using PHP

Comments are closed.