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条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看