1. 程式人生 > >swoole之非同步讀寫

swoole之非同步讀寫

//        非同步讀取檔案內容
swoole_async_readfile(__DIR__."/1.txt", function($filename, $content) {
    echo "$filename: $content";
});

//非同步寫入檔案

$file_content = '佛冷';
swoole_async_writefile(__DIR__.'/test.log', $file_content.PHP_EOL, function($filename) {
    echo $filename."wirte ok.\n";
}, FILE_APPEND);
//http_server中寫入傳來引數
$http->on('request', function ($request, $response) {
//    非同步檔案寫入
        $file_content = [
            'data' => date('Y-m-d H:i:s'),
            'get' => $request->get,
            'post' => $request->post,
            'header' => $request->header,
        ];
        swoole_async_writefile(__DIR__.'/access.log', json_encode($file_content).PHP_EOL, function($filename) {
            echo $filename.":wirte ok.\n";
        }, FILE_APPEND);

    $response->end("love".json_encode($request->get));
});

 

瀏覽器訪問時不需帶靜態檔案