"Laravel 10 - API金鑰在.env檔中未被辨識"
P粉198814372
P粉198814372 2023-08-30 11:26:11
0
1
444
<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▕ * 使用給定的 API 令牌建立新的 Open AI 用戶端。 12▕*/ ➜ 13▕ public static function client(string $apiKey, string $organization = null): Client 14▕ { 15▕ return self::factory() 16▕ ->withApiKey($apiKey) 17▕ ->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 "Cache, Config is cleared"; })->middleware(['auth', 'admin']);</pre> <p>部署時,此腳本也會自動執行:</p> <pre class="brush:php;toolbar:false;">#!/bin/sh set -e echo "Deploying application ..." # 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 # 註: 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 "" | sudo -S service php8.1-fpm reload # Exit maintenance mode php artisan up echo "Application deployed!"</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

#
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!