使用 Workerman\Http\Client,請(qǐng)求外部接口,在回調(diào)函數(shù)中,想處理響應(yīng)結(jié)果后返回json格式數(shù)據(jù),要怎么寫(xiě)呢?
echo 就在終端輸出了,return 瀏覽器訪問(wèn)頁(yè)面是空的
<?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作為結(jié)束標(biāo)志
});
return response()->withHeaders([
"Transfer-Encoding" => "chunked",
]);
}
}
webman里用的話,先發(fā)送一個(gè)chunked頭,然后再異步發(fā)送給chunk數(shù)據(jù),類(lèi)似這樣