我有两个名为“scriptA.php”和“scriptB.php”的脚本
我需要能够从“scriptA.php”中启动“scriptB.php”并使其成为浏览器不等待“scriptB.php”完全完成的。我不想等待脚本完全完成,我只想让它自己完成。我仍然需要能够执行POST或在执行脚本时将数据从“scriptA.php”传递到“scriptB.php”。
我可以不使用exec,shell_exec或命令行的任何变体。
编辑 - 尝试cURL选项
这是我的2个脚本......
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。我打电话给scriptB.php进行测试,以确保它有效,当我直接进入页面时,它确实有效。它会正确写入文件。就在我运行scriptA.php时,它没有被执行。
答案 0 :(得分:1)
你可以通过几种方式实现这一目标:
fork
函数:http://php.net/manual/ru/function.pcntl-fork.php German
或RabbitMQ
scriptB
的cronjob(例如,您可以创建crontab将处理的特殊env
表,并在每次插入后运行scriptB
表)我希望它会有所帮助
答案 1 :(得分:0)
我最终使用cURL让它工作,所以道具给Barmar指出我正确的方向。
//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);
我从http://davidwalsh.name/curl-post
找到了这个工作示例同样在scriptB.php上,我正在做一个" $ _ GET"而不是" $ _ POST"
感谢大家的投入!