How to make asynchronous HTTP requests in PHP

后端 未结 18 2291
梦如初夏
梦如初夏 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条回答
  •  萌比男神i
    2020-11-22 02:53

    Symfony HttpClient is asynchronous https://symfony.com/doc/current/components/http_client.html.

    For example you can

    use Symfony\Component\HttpClient\HttpClient;
    
    $client = HttpClient::create();
    $response1 = $client->request('GET', 'https://website1');
    $response2 = $client->request('GET', 'https://website1');
    $response3 = $client->request('GET', 'https://website1');
    //these 3 calls with return immediately
    //but the requests will fire to the website1 webserver
    
    $response1->getContent(); //this will block until content is fetched
    $response2->getContent(); //same 
    $response3->getContent(); //same
    

提交回复
热议问题