请教 Workerman Http-Client 用法

sen110257

问题描述

使用 Workerman\Http\Client,请求外部接口,在回调函数中,想处理响应结果后返回json格式数据,要怎么写呢?
截图
echo 就在终端输出了,return 浏览器访问页面是空的
截图

464 1 3
1个回答

walkor 打赏
<?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('')); // 发送空的的chunk作为结束标志
        });
        return response()->withHeaders([
            "Transfer-Encoding" => "chunked",
        ]);
    }
}

webman里用的话,先发送一个chunked头,然后再异步发送给chunk数据,类似这样

×
🔝