克朗工作中的争论

发布于 2025-01-21 15:27:30 字数 383 浏览 2 评论 0原文

我在共享服务器上。当将争论传递给Crob工作如下:

10  *   *   *   *   /usr/bin/php /home/website/cron/my_script.php country_id=10 

这可以与PHP脚本以获取Country_id的价值:

$country_id = $_GET['country_id'];

这无效,并且Country_id是无效的:

$country_id = $argv[1]

我的理解是,第一个选项不应该使用,第二个选项应,第二个选项应该应该,但是,这是相反的。

谁能给我一个关于为什么的想法?

I am on a shared server. When passing arguments to a crob job as below:

10  *   *   *   *   /usr/bin/php /home/website/cron/my_script.php country_id=10 

This works withing the php script to get the value of the country_id:

$country_id = $_GET['country_id'];

This doesn't work and country_id is null:

$country_id = $argv[1]

My understanding is that the first option should not work and the second option should, however it the the other way around.

Can anyone give me an idea as to why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煞人兵器 2025-01-28 15:27:30

HTTP和CLI是完全不同的环境。

如果您想调整脚本,以使其在任何一个环境中都没有更改而运行,则确实可以从country_id = 10中获取数据,但是您需要自己解析,因为它不是PHP期望的标准格式。您可以使用 parse_str(),但是您需要确保实际上遵循相同的编码规则您将遵循实际的URL:

if (isset($argv[1])) {
    parse_str($argv[1], $_GET);
}
var_dump($argv, $_GET);

如果要编写一个传统上行为更传统的命令行脚本,则可以使用 getopt(),并将其称为例如:

/usr/bin/php /home/website/cron/my_script.php --country_id=10
/usr/bin/php /home/website/cron/my_script.php --country_id 10
$params = getopt('', ['country_id:']);
var_dump($params);

HTTP and CLI are completely different environments.

If you want to adapt your script so it runs without changes in either environment, you can indeed get data from country_id=10 but you need to parse it yourself since it isn't a standard format that PHP expects. You can use parse_str(), but you need to ensure you're actually following the same encoding rules that you'd follow in an actual URL:

if (isset($argv[1])) {
    parse_str($argv[1], $_GET);
}
var_dump($argv, $_GET);

If you want to write a command-line script that behaves more traditionally you can use getopt() and call it like e.g.:

/usr/bin/php /home/website/cron/my_script.php --country_id=10
/usr/bin/php /home/website/cron/my_script.php --country_id 10
$params = getopt('', ['country_id:']);
var_dump($params);
遗弃M 2025-01-28 15:27:30

(1)对于您的情况,$ argv [1]是country_id = 10,而不是10。并且请确保您也有一个半彩色来结束这一行。

(2)假设您已经在使用parse_str(用于解析ARGV数据的相当标准的练习),那么$ _get [“ xxx”]可以从参数通过命令行获得值,这是正常的。请参阅此处的官方文档

https://www.php.net/manual/manual/enul/en/ famplet.commandline.php

因此,对于下面的代码(test1.php):

<?php

parse_str(implode('&', array_slice($argv, 1)), $_GET);

echo $argv[1];
echo ",";
echo $_GET['a'];

?>

如果执行命令行:

php test1.php a=1

它将显示

a=1,1

(1) For your case, $argv[1] is country_id=10 , not 10 . and please make sure you have a semi-colon to end this line too.

(2) Assuming that you are already using parse_str (a rather standard practice for parsing argv data), then $_GET["xxx"] can get values from parameters thru command line, that's normal. See official documentation here

https://www.php.net/manual/en/features.commandline.php

So for the code below (test1.php):

<?php

parse_str(implode('&', array_slice($argv, 1)), $_GET);

echo $argv[1];
echo ",";
echo $_GET['a'];

?>

If you execute the command line:

php test1.php a=1

it will show

a=1,1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文