-1

Hey PHP developers I am newbie. Today I want to run my process.php file in the background because it takes too much time to load... Here is the code that I want to use.

$proc=new BackgroundProcess();
$proc->setCmd('exec php <BASE_PATH>/process.php hello world');
$proc->start();

And I want to add this ids=$postid&reaction=$reaction variable instead of hello world. And want to receive it with post in process.php file like this

$id =$_POST['ids'];
$type = $_POST['reaction'];

I am using this GitHub file https://github.com/pandasanjay/php-script-background-processer/blob/master/README.md

Before doing downvote answer me I am a newbie in PHP.

parsa
  • 987
  • 2
  • 10
  • 23

2 Answers2

0

You can try exec() for this. If you want to pass parameters then try like this.

//it will store logs to log_data.log
exec("php process.php $id $type >log_data.log &");

Hope this will work for you :)

Try like this

   function execInBackground() {
       //this will run in background
       exec("php process.php $id $type > /dev/null &"); 
    }
localroot
  • 566
  • 3
  • 13
0

As soon as it is not HTTP request at all, you cannot access $_GET and $_POST superglobals. The right way to receive arguments in this case, is to access the array $argv. See official documentation:

http://php.net/manual/en/reserved.variables.argv.php


UPD: And, well, if you really want to pass $_GET/$_POST params to this script executed via shell, here is a dirty trick:

$get_params_as_string = base64_encode(json_encode($_GET));
$proc=new BackgroundProcess();
$proc->setCmd("exec php <BASE_PATH>/process.php {$get_params_as_string}");
$proc->start();

And in your process.php access it like this:

$get_params = json_decode(base64_decode($argv[1]), true);

So, we are just created JSON from $_GET array. Then, as we know that JSON string contains special characters(like ", {, }, etc), and to avoid dealing with problems of escaping and unescaping, we simply encode this string as base64. It guarantees us absence of special characters in result string. Now we can use this string as a single argument, which we will pass to shell command (your BackgroundProcess). And finally, in process.php we can access this string from $args[1], then decode from base64, then decode from JSON to a regular PHP array. Here we go.

This solution is provided only for educational purpose, please don't ever do it in real life.

Bushikot
  • 783
  • 3
  • 10
  • 26