How to make asynchronous HTTP requests in PHP

后端 未结 18 2297
梦如初夏
梦如初夏 2020-11-22 02:13

Is there a way in PHP to make asynchronous HTTP calls? I don\'t care about the response, I just want to do something like file_get_contents(), but not wait for

18条回答
  •  甜味超标
    2020-11-22 02:45

    The swoole extension. https://github.com/matyhtf/swoole Asynchronous & concurrent networking framework for PHP.

    $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
    
    $client->on("connect", function($cli) {
        $cli->send("hello world\n");
    });
    
    $client->on("receive", function($cli, $data){
        echo "Receive: $data\n";
    });
    
    $client->on("error", function($cli){
        echo "connect fail\n";
    });
    
    $client->on("close", function($cli){
        echo "close\n";
    });
    
    $client->connect('127.0.0.1', 9501, 0.5);
    

提交回复
热议问题