I have 2 scripts called "scriptA.php" and "scriptB.php"
I need to be able to start "scriptB.php" from within "scriptA.php" and make it so the browser isn't waiting for "scriptB.php" to fully complete. I don't want to wait for the script to fully finish, I just want it to finish on its own. I still need to be able to do a POST or to pass data from "scriptA.php" to "scriptB.php" when the script is being executed to be ran.
I can NOT use exec, shell_exec or any variation of command line.
EDIT - Trying cURL option
Here are my 2 scripts...
scriptA.php
$url = 'scriptB.php';
$data = array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'curl');
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$result = curl_exec($ch);
curl_close($ch);
scriptB.php
ignore_user_abort(true);
set_time_limit(0);
$fp = fopen("myTexts.txt","wb");
$content = "blah ->" . $_GET['foo'];
fwrite($fp,$content);
fclose($fp);
exit;
scriptB.php isn't being called. I called scriptB.php to test to make sure it works, and when I go to the page directly it does work. it does write to the file correctly. Just when i run scriptA.php it isn't being executed.
You can do it in couple ways:
fork
functions: http://php.net/manual/ru/function.pcntl-fork.phpGerman
or RabbitMQ
scriptB
after some event in your system (for example you can create special env
table that crontab will be process and run scriptB
after each insert into this table)I hope it will help
I got it to work finally using cURL so props to Barmar for pointing me into the right direction.
//set POST variables
$url = 'scriptB.php';
$fields = array(
'foo' => urlencode('bar'),
'fname' => urlencode($first_name),
'title' => urlencode($title),
'company' => urlencode($institution),
'age' => urlencode($age),
'email' => urlencode($email),
'phone' => urlencode($phone)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
I found this working example from: http://davidwalsh.name/curl-post
Also on scriptB.php I was doing a "$_GET" instead of a "$_POST"
Thank you everyone for your input!