2

我有这个用户表单。用户将提交数据。然后使用该数据,我想将该数据发送到将在后台运行的 PHP 脚本。原因是脚本有很多睡眠等,我不想让用户等待。

所以我的问题的基础是:如何在后台运行 PHP 脚本,以及如何将参数(如 GET)传递给它?

4

4 回答 4

0

像这样的东西会起作用

http://gearman.org/index.php?id=manual:job_server

但是,为什么你的剧本中有这么多睡眠?

于 2012-10-06T03:18:28.410 回答
0

试试用AJAX,是异步的javascript和xml,有一个简单的例子:http ://www.w3schools.com/php/php_ajax_intro.asp

在 AJAX 中,页面不会刷新,结果由 JSON 或 XML 发送。

于 2012-10-06T03:25:26.660 回答
0

您可以使用exec来运行您的后台脚本。使用类似的东西

exec('php -f bg_script.php -- '.escapeshellarg($param1).' '.escapeshellarg($param2).' > /dev/null & ');

在您的脚本 bg.php 中,您可以从数组中获取传递的参数$argv

于 2012-10-06T03:51:54.407 回答
-1

超时怎么办file_get_contents()

if (isset($_GET['async'])) {

    for( $i = 0 ; $i <= 5 ; $i++ )
    {
        append_log(date('l jS \of F Y h:i:s A') . ': background process. parameter ' . $i . ': ' . $_GET[$i] . '<br />');
        sleep(1);
    }

    exit;
}

header( 'Content-type: text/html; charset=utf-8' );
$parameters = array('async' => true, 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
pseudo_async($parameters);  // this runs this php script in the backbround

echo 'Begin ...<br />';
for( $i = 0 ; $i <= 5 ; $i++ )
{
    output_buffer('appended to the log <br />');
    append_log(date('l jS \of F Y h:i:s A') . ': main process.<br />');
    sleep(1);
}
echo 'End ...<br />';


function pseudo_async($query) {

    $timeout = array('http' => array('timeout' => 0.01));
    $context = stream_context_create($timeout);

    @file_get_contents(selfurl() . '?' . http_build_query($query), false, $context);
}

function append_log($msg) {
    $file = __DIR__ . '/log.html';
    file_put_contents($file, $msg, FILE_APPEND);
}

function selfurl() {
    $pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
    if ($_SERVER["SERVER_PORT"] != "80")
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    else 
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    return $pageURL;
}

function output_buffer($str) {
    echo $str;
    flush();
    ob_flush(); 
}
于 2012-10-06T05:37:52.300 回答