Is making asynchronous HTTP requests possible with PHP?

前端 未结 5 1410
情歌与酒
情歌与酒 2020-11-27 17:42

I have a PHP script that needs to download several files from a remote server. At the moment I just have a loop downloading and processing the files with cURL, which means t

5条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 18:40

    Check out curl-easy. It supports both blocking and not-blocking requests in parallel or single request at once. Also, it is unit-tested, unlike many simple or buggy libraries.

    Disclosure: I am the author of this library. The library has it's own test suite so I'm pretty confident it is robust.

    Also, check out example of use below:

    getDefaultOptions()
        ->set(CURLOPT_TIMEOUT, 5)
        ->set(CURLOPT_RETURNTRANSFER, true);
    // Set callback function to be executed when request will be completed
    $queue->addListener('complete', function (cURL\Event $event) {
        $response = $event->response;
        $json = $response->getContent(); // Returns content of response
        $feed = json_decode($json, true);
        echo $feed['entry']['title']['$t'] . "\n";
    });
    
    $request = new cURL\Request('http://gdata.youtube.com/feeds/api/videos/XmSdTa9kaiQ?v=2&alt=json');
    $queue->attach($request);
    
    $request = new cURL\Request('http://gdata.youtube.com/feeds/api/videos/6dC-sm5SWiU?v=2&alt=json');
    $queue->attach($request);
    
    // Execute queue
    $queue->send();
    

提交回复
热议问题