安装Braintree并得到错误“未捕获的InvalidArgumentException:Dotenv:找不到环境文件.env或不可读。”

时间:2019-04-05 19:01:31

标签: php braintree-sandbox

我正在尝试让Braintree在我的本地Ubuntu环境上工作,并且遇到错误Uncaught InvalidArgumentException: Dotenv: Environment file .env not found or not readable.,我认为它与以下代码中的反斜杠有关。我不确定如何解决它,任何人都可以帮忙吗?

这是我收到的错误:

[Fri Apr 05 14:14:26.170718 2019] [php7:error] [pid 88156] [client ::1:52042] PHP Fatal error:  Uncaught InvalidArgumentException: Dotenv: Environment file .env not found or not readable. Create file with your environment settings at /var/www/html/myskinpro/includes/../example.env/.env in /var/www/html/myskinpro/vendor/vlucas/phpdotenv/src/Loader.php:78\nStack trace:\n#0 /var/www/html/myskinpro/vendor/vlucas/phpdotenv/src/Loader.php(51): Dotenv\\Loader->ensureFileIsReadable()\n#1 /var/www/html/myskinpro/vendor/vlucas/phpdotenv/src/Dotenv.php(41): Dotenv\\Loader->load()\n#2 /var/www/html/myskinpro/includes/braintree_init.php(7): Dotenv\\Dotenv->load()\n#3 /var/www/html/myskinpro/payusd/index.php(1): require_once('/var/www/html/m...')\n#4 {main}\n  thrown in /var/www/html/myskinpro/vendor/vlucas/phpdotenv/src/Loader.php on line 78

这是导致问题的代码:

<?php
session_start();
require_once("../vendor/autoload.php");

if(file_exists(__DIR__ . "/../example.env")) {
    $dotenv = new Dotenv\Dotenv(__DIR__ . "/../example.env");
    $dotenv->load();
} else {
  echo "file does not exist";
}

$gateway = new Braintree\Gateway([
    'environment' => getenv('BT_ENVIRONMENT'),
    'merchantId' => getenv('BT_MERCHANT_ID'),
    'publicKey' => getenv('BT_PUBLIC_KEY'),
    'privateKey' => getenv('BT_PRIVATE_KEY')
]);

1 个答案:

答案 0 :(得分:0)

此代码解决了该问题:

if(file_exists(__DIR__ . "/../example.env")) {
    $dotenv = new Dotenv\Dotenv(__DIR__ . "/../", 'example.env');
    $dotenv->load();
} else {
  echo "file does not exist";
}