doushi2902 2015-06-18 09:51
浏览 69

异步API和回调

I have to use one asynchronous service. Everything I can do is send data to this service (I'm doing it with PHP and CURL) and send data to some url from this service. How can I react/wait for a response from this service? Now I have two pages: first is sending data to service and the second takes a response from this service and inserts it to database. On a first page I'm checking some table while there isn't the response. But selecting from database few times per second is bad idea. But what I need to have: Send data from one page and get the response at the same page. I guess I can use some Ajax and make the async service sending data to the same page and wait for the response on this page.

I guess I wrote very hard because I can't fully explain what I need, so feel free to correct me.

  • 写回答

2条回答 默认 最新

  • dqbn76906 2015-06-18 10:46
    关注

    As @Steve noted, PHP has no concept of asynchronousity. However there is a hack which allows to implement something similar to long-polling in PHP. The main point is to use a file ready to read in Javascript, i.e. JSON.

    Here is a general flow:

    1. Your single web page does AJAX request to your php script which send appropriate request to the external service and immediately return some response (e.g. empty) to the web page.
    2. The web page starts to repeatedly request server for the same static JSON file (by doing AJAX requests) until it appears (created by callback script).
    3. The external service passes response to your callback script which save the response into the JSON file.
    4. The web page get the response from the JSON file and outputs it.
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘