1.15.6.2 react/http-client

优质
小牛编辑
131浏览
2023-12-01

(要求Workerman版本>=3.3.6)

此项目为第三方项目,不建议使用。推荐使用官方异步http客户端 workerman/http-client

安装:

composer require react/http-client

示例:

<?php
require_once __DIR__ . '/vendor/autoload.php';
use WorkermanWorker;

$worker = new Worker('tcp://0.0.0.0:6161');

$worker->onMessage = function($connection, $host) {
    $loop    = Worker::getEventLoop();
    $client  = new ReactHttpClientClient($loop);
    $request = $client->request('GET', trim($host));
    $request->on('error', function(Exception $e) use ($connection) {
        $connection->send($e);
    });
    $request->on('response', function ($response) use ($connection) {
        $response->on('data', function ($data) use ($connection) {
            $connection->send($data);
        });
    });
    $request->end();
};

Worker::runAll();

文档:

https://github.com/reactphp/http-client

注意:

1、所有的异步编码必须在onXXX回调中编写

2、异步客户端需要的$loop变量请使用Worker::getEventLoop();返回值