0

当方サーバー経験なく、プログレスバーで通信状況を取得せねばならず、データ容量を取得するために
Content-Lengthヘッダー設定することで解決しようと以下のようにコードを編集しました。

$result = $games
    ->map(function($game) {
        return [
            'id' => $game->id,
            'date' => $game->date,
            'team_id' => $game->team_id,
            'team_name' => $game->team_name,
            'opponent_id' => $game->opponent_team_id,
            'opponent_team' => $game->opponent_team_name,
            'win_or_loss' => $game->win_or_loss,
            'is_field_first' => $game->is_field_first,
            'runs_scored' => $game->runs_scored,
            'runs_allowed' => $game->runs_allowed,
            'tournament' => $game->tournament,
            'is_aggregated' => $game->is_aggregated,
            'is_aggregating' => $game->is_aggregating,
            'has_draft' => $game->hasDraft(),
            'updated_at' => $game->updated_at->formatLocalized('%Y-%m-%d %H:%M:%S'),
        ];
    })
    ->values();
$gameData = [];
$gameData['games'] = $result;
return response($gameData)
    ->header('Content-Length: ' . strlen( json_encode($result->toArray()) ) );

しかし以下のエラーが出てしまい

local.ERROR: Type error: Too few arguments to function Illuminate\Http\Response::header(), 1 passed in /home/ec2-user/escore/www/app/Http/Controllers/Api/GroupController.php on line 105 and at least 2 expected {"userId":1185,"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Type error: Too few arguments to function Illuminate\\Http\\Response::header(), 1 passed in /home/ec2-user/escore/www/app/Http/Controllers/Api/GroupController.php on line 105 and at least 2 expected at /home/ec2-user/escore/www/vendor/laravel/framework/src/Illuminate/Http/ResponseTrait.php:65) 

容量の取得が出来ない状態です。
引数が足りないとエラーが出ていますが、どこでどのような設定をすればいいかわからない状態です。
コードはlaravelで作成しております。

こちら何か解決方法ご存じでしょうか。
初歩的な質問かと思いますが、よろしくお願い致します。

1 件の回答 1

1

エラーの通りheaderメソッドの引数が足りていないようです。
第一引数にはヘッダーの名前を第二引数には値を入れるようです。

return response($gameData)
    ->header('Content-Length', strlen( json_encode($result->toArray()) ) );

これに限らずですが、フレームワークやライブラリの不明点や正しいメソッドの使い方はまずはドキュメントを参考にするようにしましょう。たいていはドキュメントに使い方が書いてあることが多いです。

HTTPレスポンス5.5 Laravel

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。