duangua5308 2012-08-23 14:37
浏览 27
已采纳

如何将参数传递给脚本

I am "forking" php code, kicking off scripts to run in the background allowing the browser application to continue without having to wait for a response.

I would like to be able to pass a parameter to such a script.

Say i have an array i would like to be accessible to the script in question, how could i pass it over?

$test = array(1,2,3);

//for windows i do the following which works well enough 
pclose(popen("start php /path/to/script.php ","r"));

OR for some linux distro's :

exec('php -f /path/to/script.php >/dev/null 2>&1 &');

so firstly, is it possible to pass my array to that script? if its possible to make it accessible to the script, how could i access the passed variable in that script?

(sitting here confused, thinking about passing variable by value/reference....)

thanks!

  • 写回答

3条回答 默认 最新

  • dongxi5423 2012-08-23 15:01
    关注

    Nicolas Brown is almost right. But that would be the second part of the solution you're looking for, I think.
    As you maybe already know you can call a program from the console and yet pass any arguments the program might expect:

    >php -f /path/to/program arg1[ arg2 ... argN] >/dev/null 2>&1 &
    

    So as you're passing a string to exec() why not just "insert" your array as arguments and then execute it

    $test = array(1,2,3);
    $args = implode($test, " ");
    $prog = 'php -f /path/to/script.php ' . $args . ' >/dev/null 2>&1 &'
    exec($prog);
    

    Here comes something from the tutorial Nicolas Brown mentioned. In your script.php you can get the parameters from $argv[]

    $myParam1 = $argv[1];
    $myParam2 = $argv[2];
    

    Please, note that in argv[0] is the name of the program. That's exactly why in the tutorial they first do arry_shift($argv), to get rid off the first element (the program's name and path).
    Hope it helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办