我有一个脚本a.php,我想从我的Windows机器上的a.php调用一个(长时间运行的)脚本b.php。
这是样本文件a.php:
<?php
$command = 'start /B php b.php > NUL';
pclose(popen($command, 'r'));
?>
这是样本文件b.php
<?php
sleep(30);
?>
但这不起作用,a.php等到b.php完成。
如何在后台运行b.php?
更新
也许问题与我如何调用b.php有关,但是如何调用a.php。这是我正在做的更完整的例子:
用户调用webservice index.php?cmd = download&amp; source = s1(我用Postman测试)
的index.php:
<?php
include_once 'a.php';
if($_GET['cmd'] == "download") {
$source = $_GET['source'];
$flag = startDownload($source);
if($flag) {
echo("Download was initialized");
} else {
echo("Download not started");
}
}
?>
a.php只会:
<?php
function startDownload($source) {
if($source == "s1") {
$command = 'start /B php b.php > NUL';
pclose(popen($command, 'r'));
} else {
$command = 'start /B php c.php > NUL';
pclose(popen($command, 'r'));
}
return true;
}
?>
b.php:
<?php
$ch = curl_init();
$source = "http://example.com/bigfile.zip";
curl_setopt($ch, CURLOPT_URL, $source);
$data = curl_exec($ch);
curl_close($ch);
$file = fopen("/", "w+");
fputs($file, $data);
fclose($file);
?>