使用 Workerman\Http\Client,請求外部接口,在回調函數中,想處理響應結果后返回json格式數據,要怎么寫呢?
echo 就在終端輸出了,return 瀏覽器訪問頁面是空的
<?php
namespace app\controller;
use support\Request;
use support\Response;
use Workerman\Protocols\Http\Chunk;
class IndexController
{
public function index(Request $request)
{
$connection = $request->connection;
$http = new \Workerman\Http\Client();
$http->get('https://example.com/', function ($response) use ($connection) {
$connection->send(new Chunk($response->getBody()));
$connection->send(new Chunk('')); // 發(fā)送空的的chunk作為結束標志
});
return response()->withHeaders([
"Transfer-Encoding" => "chunked",
]);
}
}
webman里用的話,先發(fā)送一個chunked頭,然后再異步發(fā)送給chunk數據,類似這樣