How to make asynchronous HTTP requests in PHP

后端 未结 18 2378
梦如初夏
梦如初夏 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:55

    ReactPHP async http client
    https://github.com/shuchkin/react-http-client

    Install via Composer

    $ composer require shuchkin/react-http-client
    

    Async HTTP GET

    // get.php
    $loop = \React\EventLoop\Factory::create();
    
    $http = new \Shuchkin\ReactHTTP\Client( $loop );
    
    $http->get( 'https://tools.ietf.org/rfc/rfc2068.txt' )->then(
        function( $content ) {
            echo $content;
        },
        function ( \Exception $ex ) {
            echo 'HTTP error '.$ex->getCode().' '.$ex->getMessage();
        }
    );
    
    $loop->run();
    

    Run php in CLI-mode

    $ php get.php
    

提交回复
热议问题