How to make asynchronous HTTP requests in PHP

后端 未结 18 2286
梦如初夏
梦如初夏 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 03:05

    You can do trickery by using exec() to invoke something that can do HTTP requests, like wget, but you must direct all output from the program to somewhere, like a file or /dev/null, otherwise the PHP process will wait for that output.

    If you want to separate the process from the apache thread entirely, try something like (I'm not sure about this, but I hope you get the idea):

    exec('bash -c "wget -O (url goes here) > /dev/null 2>&1 &"');
    

    It's not a nice business, and you'll probably want something like a cron job invoking a heartbeat script which polls an actual database event queue to do real asynchronous events.

提交回复
热议问题