将压缩的JSON发布到Laravel API端点

时间:2019-01-07 23:10:59

标签: php .net laravel compression

我使用Apache Benchmark进行了一些基本测试,看起来好像使用DeflateStream类将500条记录从C#发布到PHP导致处理速度提高了大约3倍,带宽从140k减少到10k,而每个帖子额外增加2%的CPU。

我现在尝试更新现有的API端点,以便如果旧的连接器仍然发送未压缩的数据,则不会中断,但是发送压缩数据的更新的连接器将被自动解压缩。尽管我使用Laravel使用普通的PHP进行了概念验证,但不确定如何将其与Request类一起使用。这是我的API的启动方式:

public function BasicApi(Request $request)
{
    $messages = [
    ];

    $validator = Validator::make($request->all(), [
        'data' => 'required|array',
    ], $messages);
    if ($validator->fails()) {
        throw new UpdateResourceFailedException('Could not sync.', $validator->errors());
    }

    $reference_id = collect($request->input('data'))->pluck('reference_id');
    ........  more stuff  ........

}

使用直接的PHP,我能够找到一种方法来过滤传入代码是否已压缩:

$incomingData = gzinflate(file_get_contents('php://input')); 
if (file_get_contents('php://input') != $incomingData && $incomingData !== FALSE) {
     $incomingData = gzinflate(file_get_contents('php://input'));
}
echo $source;

我应该怎么做才能发送我的Laravel API压缩数据?如果我让Apache自动解压缩,那么请求将像这样逃脱:

"{\"data\":[{\"reference_id\":\"75080\"...

如果我不让它自动执行,那么我不确定如何将传入内容注入Request对象中。

0 个答案:

没有答案