I'm trying to use the OpenAI API client for PHP, following these instructions
Installing the libraries seemed to go fine:
composer require openai-php/client
composer require guzzlehttp/guzzle
And the relevant initial fragment of PHP:
$yourApiKey = getenv('YOUR_API_KEY');
$client = OpenAI::client($yourApiKey);
Which gets this error:
Fatal error: Uncaught Error: Class "OpenAI" not found in C:\xampp\htdocs\test.php:13 Stack trace: #0 {main} thrown in C:\xampp\htdocs\test.php on line 13
Looking at answers to adjacent questions, there is the insertion of an initial line of code:
require_once 'vendor/autoload.php';
Tried; no difference.
Some people seem to be saying the last line needs to be modified with an explicit scope prefix:
$client = \OpenAI::client($yourApiKey);
Tried; no difference.
What am I missing?
Fix found! I had my code in C:\xampp\htdocs
as the simplest way to start. Maybe some of the other stuff XAMPP puts there by default was confusing PHP, for creating a subdirectory C:\xampp\htdocs\openai
and putting my code (test.php
and the vendor
directory and associated files created by Composer) into it, caused the test to succeed.