我的工作是一个 PHP 网络应用程序,我需要执行一些网络操作的请求,如从远程服务器的人根据用户的要求。
是否可以在 PHP 中模拟异步行为,因为我必须将一些数据传递给函数,并且还需要从函数中输出。
我的代码是这样的:
<?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;
?>
每个网络操作大约需要5秒钟才能完成,如果我发出3个请求,那么我的应用程序的响应时间总共要增加15秒。
MakeNetworkCall ()函数只是执行一个 HTTPPOST 请求。
远程服务器是第三方 API,所以我在那里没有任何控制权。
附注: 请不要回答给出关于 AJAX 或其他东西的建议。我目前正在寻找,如果我可以做到这一点,通过 PHP 可能是一个 C + + 扩展或类似的东西。