"Laravel 10 - API密钥在.env文件中未被识别"
P粉198814372
P粉198814372 2023-08-30 11:26:11
[PHP讨论组]
<p>我正在使用 <code>Laravel Framework 10.15.0</code>。</p> <p>我尝试以以下方式加载我的API密钥:</p> <pre class="brush:php;toolbar:false;">$apiKeyOpenAI = env('OPENAI_API_KEY'); $client = OpenAI::client($apiKeyOpenAI);</pre> <p>在我的 <code>.env</code> 文件中,API密钥已经明确定义:</p> <p><code>OPENAI_API_KEY=xx-xxxxxxxxxxxxxxxxxxxxxxx</code></p> <p>然而,在服务器上执行我的应用程序时,我得到的 <code>$apiKeyOpenAI</code> 是null。</p> <p>但是,我的 <code>.env</code> 文件中确实有 OPENAI_API_KEY。我已经检查过了!</p> <p>我尝试清除缓存 <code>php artisan config:clear </code>,但仍然出现错误:</p> <pre class="brush:php;toolbar:false;">TypeError OpenAI::client(): Argument #1 ($apiKey) must be of type string, null given, called in /var/www/demo-website/app/Console/Commands/AdminCommand.php on line 151 at vendor/openai-php/client/src/OpenAI.php:13 9▕ { 10▕ /** 11▕ * Creates a new Open AI Client with the given API token. 12▕ */ ➜ 13▕ public static function client(string $apiKey, string $organization = null): Client 14▕ { 15▕ return self::factory() 16▕ -&gt;withApiKey($apiKey) 17▕ -&gt;withOrganization($organization) 1 app/Console/Commands/AdminCommand.php:151 OpenAI::client() 2 app/Console/Commands/AdminCommand.php:39 App\Console\Commands\AdminCommand::generateContentUsingOpenAI()</pre> <p>有什么建议我做错了吗?</p> <p>感谢您的回复!</p> <p><strong>更新</strong></p> <p>在部署到服务器后,我需要运行此脚本以使其正常工作:</p> <pre class="brush:php;toolbar:false;">Route::get('/clear', function() { Artisan::call('cache:clear'); Artisan::call('config:clear'); return &quot;Cache, Config is cleared&quot;; })-&gt;middleware(['auth', 'admin']);</pre> <p>在部署时,此脚本也会自动运行:</p> <pre class="brush:php;toolbar:false;">#!/bin/sh set -e echo &quot;Deploying application ...&quot; # Enter maintenance mode (php artisan down) || true # Update codebase git fetch origin deploy git reset --hard origin/deploy # Install dependencies based on lock file composer install --no-interaction --prefer-dist --optimize-autoloader # Migrate database php artisan migrate --force # Note: If you're using queue workers, this is the place to restart them. # ... # Clear cache # php artisan optimize php artisan config:cache php artisan route:clear php artisan route:cache php artisan view:clear php artisan view:cache php artisan auth:clear-resets php artisan cache:clear php artisan config:clear # Generate sitemap # php artisan sitemap:generate # Reload PHP to update opcache echo &quot;&quot; | sudo -S service php8.1-fpm reload # Exit maintenance mode php artisan up echo &quot;Application deployed!&quot;</pre></p>
P粉198814372
P粉198814372

全部回复(1)
P粉317679342

config/*.php文件之外不要使用env()。如果你曾经运行过php artisan config:cache(通常应该在生产环境中进行),那么env()将停止在这些文件之外工作(对于大多数情况来说;env键仍然可以加载,但这对于大多数Laravel设置来说并不典型)。这就是为什么你需要运行php artisan config:clear才能使env()不返回null的原因。

config/app.php(或config/目录下的任何其他文件)中添加一个键:

'open_ai_api_key' => env('OPENAI_API_KEY', null)

然后,当你想要使用这个键时,使用config()辅助函数:

$apiKeyOpenAI = config('app.open_ai_api_key');
$client = OpenAI::client($apiKeyOpenAI);

注意:app是文件名,open_ai_api_key是数组索引。如果你使用了不同的文件,比如config/services.php,那么应该使用config('services.open_ai_api_key')

详细信息请参阅文档:

https://laravel.com/docs/10.x/configuration#configuration-caching

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号