gpt4 book ai didi

php - 在 Go 中获取 PHP 配置变量

转载 作者:IT王子 更新时间:2023-10-29 01:33:33 26 4
gpt4 key购买 nike

我想获取命令 php -r 'echo get_cfg_var("some_var");' 的输出,以根据预定义值检查它。目前,我有以下代码:

variableName := "default_mimetype"
cmd := exec.Command("php", "-r", "'echo get_cfg_var(\"" + variableName + "\");'")
out, err := cmd.CombinedOutput()

运行后,

err.Error() 返回 "exit status 254"

out“PHP Parse error: syntax error, unexpected end of file in Command line code on line 1”

是什么导致了这个错误?我没有正确转义吗?

最佳答案

问题是你的论点。如果您将写入的内容更改为 shell 命令,它将如下所示:

$ php -r "'echo get_cfg_var(\"default_mimetype\");'"

您会注意到第二个参数周围有一组额外的引号导致语法错误。您可以通过将 exec.Command 更改为以下内容来解决此问题:

cmd := exec.Command("php", "-r", "echo get_cfg_var(\"" + variableName + "\");")

关于php - 在 Go 中获取 PHP 配置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30875893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com