"Laravel 10 - API key not recognized in .env file"
P粉198814372
P粉198814372 2023-08-30 11:26:11
0
1
428
<p>I'm using <code>Laravel Framework 10.15.0</code>. </p> <p>I tried loading my API key in the following way: </p> <pre class="brush:php;toolbar:false;">$apiKeyOpenAI = env('OPENAI_API_KEY'); $client = OpenAI::client($apiKeyOpenAI);</pre> <p>In my <code>.env</code> file, the API key is clearly defined: </p> <p><code>OPENAI_API_KEY=xx-xxxxxxxxxxxxxxxxxxxxxxx</code></p> <p>However, when executing my application on the server, I get that <code>$apiKeyOpenAI</code> is null. </p> <p>However, I do have OPENAI_API_KEY in my <code>.env</code> file.I've checked it! </p> <p>I tried clearing the cache <code>php artisan config:clear </code> but I still get the error: </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▕ ->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>Any suggestions what I'm doing wrong? </p> <p>Thanks for your reply! </p> <p><strong>Update</strong></p> <p>After deploying to the server, I need to run this script to make it work: </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>On deployment, this script will also run automatically: </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 # 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 "" | sudo -S service php8.1-fpm reload # Exit maintenance mode php artisan up echo "Application deployed!"</pre></p>
P粉198814372
P粉198814372

reply all(1)
P粉317679342

Do not use env() outside of config/*.php files. If you ever run php artisan config:cache (which should usually be done in a production environment) then env() will stop working outside of these files (for most cases says; the env key can still be loaded, but this is not typical for most Laravel setups). This is why you need to run php artisan config:clear so that env() does not return null.

Add a key in config/app.php (or any other file in the config/ directory):

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

Then, when you want to use this key, use config()Helper function:

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

Note: app is the file name, open_ai_api_key is the array index. If you use a different file, such as config/services.php, then you should use config('services.open_ai_api_key')

Please refer to the documentation for details:

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!