1. Go to this page and download the library: Download webman/openai library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
webman / openai example snippets
ebman\Openai\Chat;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\Response;
use Workerman\Events\Fiber;
use Workerman\Worker;
$worker = new Worker('http://0.0.0.0:8686');
$worker->eventLoop = Fiber::class;
$worker->onMessage = function (TcpConnection $connection, Request $request) {
$chat = new Chat([
'apikey' => getenv('OPENAI_API_KEY') ?: 'sk-xxx',
'api' => 'https://api.openai.com',
]);
$result = $chat->completions([
'model' => 'gpt-4o-mini',
'messages' => [['role' => 'user', 'content' => 'hello']],
]);
$connection->send(new Response(200, [
'Content-Type' => 'application/json; charset=utf-8',
], json_encode($result, JSON_UNESCAPED_UNICODE)));
};
Worker::runAll();
ebman\Openai\Chat;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Chunk;
use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\Response;
use Workerman\Events\Fiber;
use Workerman\Worker;
$worker = new Worker('http://0.0.0.0:8686');
$worker->eventLoop = Fiber::class;
$worker->onMessage = function (TcpConnection $connection, Request $request) {
$chat = new Chat([
'apikey' => getenv('OPENAI_API_KEY') ?: 'sk-xxx',
'api' => 'https://api.openai.com',
]);
$chunks = $chat->completions([
'model' => 'gpt-4o-mini',
'stream' => true,
'messages' => [['role' => 'user', 'content' => 'hello']],
]);
$connection->send(new Response(200, [
'Transfer-Encoding' => 'chunked',
'Content-Type' => 'application/x-ndjson; charset=utf-8',
]));
foreach ($chunks as $chunk) {
$connection->send(new Chunk(json_encode($chunk, JSON_UNESCAPED_UNICODE) . "\n"));
}
$connection->close(new Chunk(''));
};
Worker::runAll();
namespace app\controller;
use support\Request;
use Webman\Openai\Chat;
class ChatController
{
public function completions(Request $request)
{
$chat = new Chat([
'apikey' => getenv('OPENAI_API_KEY') ?: 'sk-xxx',
'api' => 'https://api.openai.com',
]);
$result = $chat->completions([
'model' => 'gpt-4o-mini',
'messages' => [['role' => 'user', 'content' => 'hello']],
]);
return json($result);
}
}
namespace app\controller;
use support\Request;
use Webman\Openai\Chat;
use Workerman\Protocols\Http\Chunk;
class ChatController
{
public function completions(Request $request)
{
$chat = new Chat([
'apikey' => getenv('OPENAI_API_KEY') ?: 'sk-xxx',
'api' => 'https://api.openai.com',
]);
$chunks = $chat->completions([
'model' => 'gpt-4o-mini',
'stream' => true,
'messages' => [['role' => 'user', 'content' => 'hello']],
]);
$connection = $request->connection;
$connection->send(response()->withHeaders([
'Transfer-Encoding' => 'chunked',
'Content-Type' => 'application/x-ndjson; charset=utf-8',
]));
foreach ($chunks as $chunk) {
$connection->send(new Chunk(json_encode($chunk, JSON_UNESCAPED_UNICODE) . "\n"));
}
$connection->close(new Chunk(''));
// Stream finished manually; no return needed
}
}
namespace app\controller;
use support\Request;
use Webman\Openai\Chat;
use Webman\Openai\OpenAIException;
use Workerman\Http\Response;
use Workerman\Protocols\Http\Chunk;
class ChatController
{
public function completions(Request $request)
{
$connection = $request->connection;
$chat = new Chat([
'apikey' => getenv('OPENAI_API_KEY') ?: 'sk-xxx',
'api' => 'https://api.openai.com',
]);
$chat->completions(
[
'model' => 'gpt-4o-mini',
'stream' => true,
'messages' => [['role' => 'user', 'content' => 'hello']],
],
[
'stream' => function (array $chunk) use ($connection) {
$connection->send(new Chunk(json_encode($chunk, JSON_UNESCAPED_UNICODE) . "\n"));
},
'complete' => function (?array $result, ?OpenAIException $e, ?Response $response) use ($connection) {
$connection->send(new Chunk(''));
},
]
);
return response()->withHeaders([
'Transfer-Encoding' => 'chunked',
'Content-Type' => 'application/x-ndjson; charset=utf-8',
]);
}
}
namespace app\controller;
use support\Request;
use Webman\Openai\Chat;
use Webman\Openai\OpenAIException;
use Workerman\Http\Response;
use Workerman\Protocols\Http\Chunk;
class ChatController
{
public function completions(Request $request)
{
$connection = $request->connection;
$chat = new Chat([
'apikey' => getenv('OPENAI_API_KEY') ?: 'sk-xxx',
'api' => 'https://api.openai.com',
]);
$chat->completions(
[
'model' => 'gpt-4o-mini',
'messages' => [['role' => 'user', 'content' => 'hello']],
],
[
'complete' => function (?array $result, ?OpenAIException $e, ?Response $response) use ($connection) {
$connection->send(new Chunk(json_encode($result, JSON_UNESCAPED_UNICODE)));
$connection->send(new Chunk(''));
},
]
);
return response()->withHeaders([
'Transfer-Encoding' => 'chunked',
'Content-Type' => 'application/json; charset=utf-8',
]);
}
}
namespace app\controller;
use support\Request;
use Webman\Openai\Chat;
class ChatController
{
private function runTool(string $name, array $args): string
{
if ($name === 'get_weather') {
$city = $args['city'] ?? '';
return json_encode([
'city' => $city,
'summary' => 'Clear',
'temp_c' => 22,
], JSON_UNESCAPED_UNICODE);
}
return json_encode(['error' => 'unknown tool'], JSON_UNESCAPED_UNICODE);
}
public function completions(Request $request)
{
$chat = new Chat([
'apikey' => getenv('OPENAI_API_KEY') ?: 'sk-xxx',
'api' => 'https://api.openai.com',
]);
$tools = [
[
'type' => 'function',
'function' => [
'name' => 'get_weather',
'description' => 'Get the current weather for a given city',
'parameters' => [
'type' => 'object',
'properties' => [
'city' => ['type' => 'string'],
],
'' => 'gpt-4o-mini',
'messages' => $messages,
'tools' => $tools,
]);
return json([
'first' => $first,
'second' => $second,
]);
}
return json($first);
}
}
namespace app\controller;
use support\Request;
use Webman\Openai\Image;
class ImageController
{
public function generations(Request $request)
{
$image = new Image([
'apikey' => getenv('OPENAI_API_KEY') ?: 'sk-xxx',
'api' => 'https://api.openai.com',
]);
$result = $image->generations([
'model' => 'dall-e-3',
'prompt' => 'a dog',
'n' => 1,
'size' => '1024x1024',
]);
return json($result);
}
}
namespace app\controller;
use support\Request;
use Webman\Openai\Image;
use Webman\Openai\OpenAIException;
use Workerman\Http\Response;
use Workerman\Protocols\Http\Chunk;
class ImageController
{
public function generations(Request $request)
{
$connection = $request->connection;
$image = new Image([
'apikey' => getenv('OPENAI_API_KEY') ?: 'sk-xxx',
'api' => 'https://api.openai.com',
]);
$image->generations(
[
'model' => 'dall-e-3',
'prompt' => 'a dog',
'n' => 1,
'size' => '1024x1024',
],
[
'complete' => function (?array $result, ?OpenAIException $e, ?Response $response) use ($connection) {
$connection->send(new Chunk(json_encode($result, JSON_UNESCAPED_UNICODE)));
$connection->send(new Chunk(''));
},
]
);
return response()->withHeaders([
'Content-Type' => 'application/json; charset=utf-8',
'Transfer-Encoding' => 'chunked',
]);
}
}
namespace app\controller;
use support\Request;
use Webman\Openai\Embedding;
class EmbeddingController
{
public function create(Request $request)
{
$embedding = new Embedding([
'apikey' => getenv('OPENAI_API_KEY') ?: 'sk-xxx',
'api' => 'https://api.openai.com',
]);
$result = $embedding->create([
'model' => 'text-embedding-3-small',
'input' => 'Some words',
'encoding_format' => 'float',
]);
return json($result);
}
}
namespace app\controller;
use support\Request;
use Webman\Openai\Embedding;
use Webman\Openai\OpenAIException;
use Workerman\Http\Response;
use Workerman\Protocols\Http\Chunk;
class EmbeddingController
{
public function create(Request $request)
{
$connection = $request->connection;
$embedding = new Embedding([
'apikey' => getenv('OPENAI_API_KEY') ?: 'sk-xxx',
'api' => 'https://api.openai.com',
]);
$embedding->create(
[
'model' => 'text-embedding-3-small',
'input' => 'Some words',
'encoding_format' => 'float',
],
[
'complete' => function (?array $result, ?OpenAIException $e, ?Response $response) use ($connection) {
$connection->send(new Chunk(json_encode($result, JSON_UNESCAPED_UNICODE)));
$connection->send(new Chunk(''));
},
]
);
return response()->withHeaders([
'Content-Type' => 'application/json; charset=utf-8',
'Transfer-Encoding' => 'chunked',
]);
}
}
namespace app\controller;
use support\Request;
use Webman\Openai\Audio;
class AudioController
{
public function speech(Request $request)
{
$audio = new Audio([
'apikey' => getenv('OPENAI_API_KEY') ?: 'sk-xxx',
'api' => 'https://api.openai.com',
]);
$binary = $audio->speech([
'model' => 'gpt-4o-mini-tts',
'input' => 'Hello, how can I help you?',
'voice' => 'alloy',
]);
return response($binary)->withHeaders([
'Content-Type' => 'audio/mpeg',
]);
}
}
namespace app\controller;
use support\Request;
use Webman\Openai\Audio;
use Workerman\Protocols\Http\Chunk;
class AudioController
{
public function speechStream(Request $request)
{
$connection = $request->connection;
$audio = new Audio([
'apikey' => getenv('OPENAI_API_KEY') ?: 'sk-xxx',
'api' => 'https://api.openai.com',
]);
$chunks = $audio->speech([
'model' => 'gpt-4o-mini-tts',
'input' => 'Hello, how can I help you?',
'voice' => 'alloy',
'stream' => true,
]);
$connection->send(response()->withHeaders([
'Content-Type' => 'audio/mpeg',
'Transfer-Encoding' => 'chunked',
]));
foreach ($chunks as $buffer) {
$connection->send(new Chunk((string) $buffer));
}
$connection->close(new Chunk(''));
}
}
namespace app\controller;
use support\Request;
use Webman\Openai\Audio;
use Webman\Openai\OpenAIException;
use Workerman\Http\Response;
use Workerman\Protocols\Http\Chunk;
class AudioController
{
public function speech(Request $request)
{
$connection = $request->connection;
$audio = new Audio([
'apikey' => getenv('OPENAI_API_KEY') ?: 'sk-xxx',
'api' => 'https://api.openai.com',
]);
$audio->speech(
[
'model' => 'gpt-4o-mini-tts',
'input' => 'Hello, how can I help you?',
'voice' => 'alloy',
],
[
'stream' => function (string $buffer) use ($connection) {
$connection->send(new Chunk($buffer));
},
'complete' => function (?string $result, ?OpenAIException $e, ?Response $response) use ($connection) {
$connection->send(new Chunk(''));
},
]
);
return response()->withHeaders([
'Content-Type' => 'audio/mpeg',
'Transfer-Encoding' => 'chunked',
]);
}
}
namespace app\controller;
use support\Request;
use Webman\Openai\Audio;
class AudioController
{
public function transcribe(Request $request)
{
$audio = new Audio([
'apikey' => getenv('OPENAI_API_KEY') ?: 'sk-xxx',
'api' => 'https://api.openai.com',
]);
$result = $audio->transcriptions([
'model' => 'gpt-4o-mini-transcribe',
'file' => '/path/to/audio.mp3',
// 'response_format' => 'json', // default JSON; use 'text' for plain-text responses, etc.
]);
// Usually ['text' => '...', ...] for JSON; string when the response body is plain text
return json($result);
}
}
namespace app\controller;
use support\Request;
use Webman\Openai\Audio;
class AudioController
{
public function transcribeStream(Request $request)
{
$audio = new Audio([
'apikey' => getenv('OPENAI_API_KEY') ?: 'sk-xxx',
'api' => 'https://api.openai.com',
]);
$events = $audio->transcriptions([
'model' => 'gpt-4o-mini-transcribe',
'file' => [
'contents' => (string) file_get_contents('/path/to/audio.mp3'),
'filename' => 'clip.mp3',
'mime' => 'audio/mpeg',
],
'stream' => true,
]);
$lines = [];
foreach ($events as $ev) {
$lines[] = $ev;
}
return json(['events' => $lines]);
}
}
namespace app\controller;
use support\Request;
use Webman\Openai\Audio;
use Webman\Openai\OpenAIException;
use Workerman\Http\Response;
class AudioController
{
public function transcribeAsync(Request $request)
{
$audio = new Audio([
'apikey' => getenv('OPENAI_API_KEY') ?: 'sk-xxx',
'api' => 'https://api.openai.com',
]);
$audio->transcriptions(
[
'model' => 'gpt-4o-mini-transcribe',
'file' => '/path/to/audio.mp3',
],
[
'complete' => function (array|string|null $result, ?OpenAIException $e, ?Response $response) {
// On success: $result is array|string; on failure: $e is non-null
},
]
);
return json(['ok' => true, 'note' => 'Handle the result inside the complete callback']);
}
}
namespace app\controller;
use support\Request;
use Webman\Openai\Audio;
use Webman\Openai\OpenAIException;
use Workerman\Http\Response;
class AudioController
{
public function transcribeStreamAsync(Request $request)
{
$audio = new Audio([
'apikey' => getenv('OPENAI_API_KEY') ?: 'sk-xxx',
'api' => 'https://api.openai.com',
]);
$audio->transcriptions(
[
'model' => 'gpt-4o-mini-transcribe',
'file' => '/path/to/audio.mp3',
'stream' => true,
],
[
'stream' => function (array $event) {
// e.g. transcript.text.delta / transcript.text.done
},
'complete' => function (array|string|null $result, ?OpenAIException $e, ?Response $response) {
// If stream is set above, $result is often null on success.
// If only complete + $data['stream'], $result is an aggregated array (