guzzle / guzzle

Guzzle, an extensible PHP HTTP client

Home Page:https://docs.guzzlephp.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Under PHP-FPM Mode, stream request will be blocked

ramzeng opened this issue · comments

Guzzle version(s) affected: 7.5.0
PHP version:8.1.17
cURL version: 7.74.0

Description

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.openai.com/v1/chat/completions', options: [
    'stream' => true,
    'headers' => [
        'Content-Type' => 'application/json',
        'Authorization' => 'Bearer {openai api key}',
    ],
    'json' => [
        'stream' => true,
        'model' => 'gpt-3.5-turbo',
        'messages' => [
            [
                'role' => 'user',
                'content' => 'Hi'
            ]
        ],
    ],
]);

$body = $response->getBody();

while (!$body->eof()) {
    echo $body->read(1024);
    ob_flush();
    flush();
}

it seems to wait for all data return from 'openai'.

but if i use the laravel artisan serve mode, it will return soon. and i can echo them one by one.

Could you give me some advice? thank you very much.