Shell run/execute php script with parameters

前端 未结 5 1774
独厮守ぢ
独厮守ぢ 2020-12-02 22:59

I need to execute a php file with parameters through shell.

here is how I would run the php file:

php -q htdocs/file.php

5条回答
  •  时光说笑
    2020-12-02 23:16

    If you have webserver (not only just php interpreter installed, but LAMP/LNMP/etc) - just try this

    wget -O - -q -t 1 "http://mysite.com/file.php?show=show_name" >/dev/null 2>&1
    

    where:

    • « -O - » — (Letter "O", not zero!) redirect "downloaded html" to stdout
    • « >/dev/null 2>&1 » — redirect stdout & stderr output to nowhere
    • « -q » — quiet wget run
    • « -t 1 » — just 1 try to connect (not like default 20)

    In PHP's "exec" it'll be smth like this:

    function exec_local_url($url) {
      exec('/usr/bin/wget -O - -q -t 1 "http://'. $_SERVER['HTTP_HOST'] .'/'
        . addslashes($url) . '" >/dev/null 2>&1'
      );
    }
    
    // ...
    
    exec_local_url("file.php?show=show_name");
    exec_local_url("myframework/seo-readable/show/show_name");
    

    So, you don't need to change your scripts to handle argc/argv, and may use $_GET as usually do.

    If you want jobs runned in background - see for ex. Unix/Windows, Setup background process? from php code

    I use approach with wget in my cron jobs; hope it helps.

提交回复
热议问题