dou9022 2013-01-09 13:27
浏览 68

PHP中的异步函数调用

I am working on an a PHP web application and i need to perform some network operations in the request like fetching someone from remote server based on user's request.

Is it possible to simulate asynchronous behavior in PHP given that i have to pass some data to a function and also need output from it.

My code is like:

<?php

     $data1 = processGETandPOST();
     $data2 = processGETandPOST();
     $data3 = processGETandPOST();

     $response1 = makeNetworkCall($data1);
     $response2 = makeNetworkCall($data2);
     $response3 = makeNetworkCall($data3);

     processNetworkResponse($response1);
     processNetworkResponse($response2);
     processNetworkResponse($response3);

     /*HTML and OTHER UI STUFF HERE*/

     exit;
?>

Each network operation takes around 5 seconds to complete adding a total of 15 seconds to the response time of my application given i make 3 requests.

The makeNetworkCall() function just do a HTTP POST request.

The remote server is an 3rd party API so i don't have any control over there.

PS: Please do not answer giving suggestions about AJAX or Other things. I am currently looking if i can do this through PHP may be with an C++ extension or something like that.

  • 写回答

6条回答 默认 最新

  • doufangxian4985 2013-01-09 13:34
    关注

    cURL is going to be your only real choice here (either that, or using non-blocking sockets and some custom logic).

    This link should send you in the right direction. There is no asynchronous processing in PHP, but if you're trying to make multiple simultaneous web requests, cURL multi will take care of that for you.

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料