dongni8124 2013-05-17 10:30
浏览 538
已采纳

PHP get_file_contents是异步的

I just wondered if anyone had managed to get the contents of a remote URL asynchronously, work on it and then output it to the current document without using AJAX.

  • 写回答

3条回答 默认 最新

  • dqzpt40064 2013-05-17 12:53
    关注

    Not with file_get_contents, no. Alternatively you can use the CURL extension and their non-blocking IO capabilities. All of this is available via the Multi Handlers of CURL. It's 'async' in the sense that you may poll the result and continue processing on other things if the result isn't ready to be read. This makes it 'async' in the sense that you're never blocking and waiting for the response, as long as there's work to be done - do it.. What's hard to understand for some people is the difference between async and non-blocking, in most cases with PHP what you really want is some sort of NB I/O. Given a few abstraction layers you can really come up with a nice IO callback interface with all the fun of anon functions, but thats another story..

    See http://www.php.net/manual/en/function.curl-multi-init.php this is where it starts, the example included is great See http://www.php.net/manual/en/function.curl-multi-exec.php See http://www.php.net/manual/en/function.curl-multi-select.php for the NB poller

    This guy avoids the ease of curl and goes straight to handling the http protocol which is yet another solution https://segment.io/blog/how-to-make-async-requests-in-php/ but requires a bit more of knowledge of the socket api.

    Cheers

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型