使用 azure cli 的 php exec 命令始终返回 null

标签 php azure apache exec

在运行 PHP 的 Apache 服务器上,我有一个始终返回 null 的命令。

exec("az storage container exists --account-name $accountName --account-key $key --name $containeurName", $output);

我修改了 sudoers 文件,但它没有改变任何内容:

www-data ALL=(ALL) NOPASSWD: /usr/bin/az

感谢您的帮助。

最佳答案

使用 exec($your_command, $output, $error_code) 并查看 $error_code 包含什么内容。这可能只是因为 az 不在 PHP 的 PATH 环境变量中。

尝试输入可执行文件的完整路径,通常如下所示:

<?php

// A default path in case "which az" doesn't work.
define('AZ_DEFAULT_PATH', '/usr/bin/az');

// Find the path to az with the "which" command.
$az_path = exec('which az');
if ($az_path === false) {
  $az_path = AZ_DEFAULT_PATH;
}

// Sanitize your variables to avoid shell injection and build the command.
$cmd = $az_path .
       "storage container exists --account-name $accountName " .
       "--account-key $key --name $containeurName";

$last_line = exec($cmd, $full_output, $error_code);

// Then check if $last_line !== false and check $error_code to see
// what happened.
var_export([
  '$cmd' => $cmd,
  '$last_line' => $last_line,
  '$full_output' => $full_output,
  '$error_code' => $error_code,
]);

关于使用 azure cli 的 php exec 命令始终返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71529296/

相关文章:

javascript - Laravel-Vuejs : Remove repeated time slots

php - 尝试在测试函数 Laravel 6 中使用配置值

azure - 使用 Terraform(或 ARM 模板)为 Webtest 创建 Azure 警报规则?

c# - 来自 Azure Blob 下载图像的链接,但我只需要观看(Asp.NET Core)

Php - 获取 php 脚本后的路径

php - Apache Windows 卸载路径

javascript - React-Native fetch 不将正文内容发送到 $_POST

php - YouTube 错误 HTTP 429 - 请求过多

c# - Azure GraphClient 库从组中删除用户

apache - 如何在不导致无限循环的情况下将所有请求重写为单个文件