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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料