doujishan2247 2013-08-20 07:05
浏览 154
已采纳

PHP:http GET / POST以真正的异步方式

Basically, I'm looking for a way to do the following in PHP:

http_get_or_post('an.url', 'or.two');

// Do some work here, not worrying about the http going on in the background.

$r = wait_for_and_get_the_results_of_the_http_requests()

And maybe someone with more curl experience can confirm that curl_multi is what I'm looking for.

From what I gather from http://php.net/manual/en/function.curl-multi-init.php, the sample there might give me what I need:

$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
$mh = curl_multi_init();
curl_multi_add_handle($mh,$ch1);

$active = null;
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

// Now, am I free to do some time consuming work here and not worry about
// calling curl_multi_exec every now and then to facilitate the background
// http / socket processes?

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}

curl_multi_remove_handle($mh, $ch1);
curl_multi_close($mh);

Now, the main question is, is my understanding of what this would do correct?

1) The first loop will only take care of sending out / writing the requests to the sockets.

2) All of the http / socket stuff will happen in the background after the requests have been sent, leaving me free to do other stuff without having to periodically call curl_multi_exec to make sure that some buffer is not full somewhere and needs a kick to keep going.

3) The second loop will wait for any outstanding response data to arrive and finish reading and processing the responses.

Now, this would still not be fully async - I might become blocked on writing the requests should the socket write buffers fill up, but in my scenario that's not a problem, I'm only worried about having to call curl_multi_exec while I'm doing the other stuff in the middle, just so the whole thing would not freeze until the next chance I get to call curl_multi_exec.

I'm also fine with this being the case for the general scenario of 2k-4k responses, with bigger responses getting stuck doing nothing in the background until I get to the second loop.

Is this how curl_multi works? If not, what can you suggest that will get this done in PHP?

  • 写回答

2条回答

  • dongpao5127 2013-08-20 07:50
    关注

    curl_multi does its work a very small piece at a time. Every call to curl_multi_exec does a little fragment of the transfer operation and once you've called it enough number of times and the data has been sent and received, it is done.

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

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建