2

古いシステムを新しいバージョンの Laravel に移行していますが、リクエストの 1 つで問題が発生しています...

基本的に、このリクエストでは、ファイルを受け取り、それをユーザーに転送するだけです。を使用した古いバージョンは次のGuzzleとおりです。

use Symfony\Component\HttpFoundation\StreamedResponse;

public function getMedia($media)
{
    try {
        $response = $this->client->get('media/' . $media, [
                'stream' => true
            ]
        );

        $contentType = $response->getHeader('Content-Type');

        $body = $response->getBody();

        $stream = new StreamedResponse(function () use ($body) {
            while (!$body->eof()) {
                echo $body->read(1024);
            }
        });

        $stream->headers->set('Content-Type', $contentType);

        return $stream;
    } catch (ClientException $e) {
        return response()->json([
            'errors' => json_decode($e->getResponse()->getBody()->getContents())->errors,
            'message' => 'Unfortunately we could not find the requested file'
        ], 404);
    }
}

そして、私が書き込もうとした新しいコードは成功しませんでした:

use Symfony\Component\HttpFoundation\StreamedResponse;

public function getMedia($media)
{
    $response = Http::withOptions([
        'stream' => true
    ])->get("media/{$media}");

    $contentType = $response->header('Content-Type');

    $body = $response->body();

    $stream = new StreamedResponse(function () use ($body) {
        while (!$body->eof()) {
            echo $body->read(1024);
        }
    });

    $stream->headers->set('Content-Type', $contentType);

    return $stream;
}

これを解決する方法を知っている人はいますか?もうどうしたらいいのかわからない…

4

0 に答える 0