php - 如何判断 curl_multi_exec 何时完成_发送_数据

标签 php curl asynchronous nonblocking

我需要从 PHP 脚本调用网络服务。 Web 服务很慢,我对其响应不感兴趣,我只想向它发送数据。

我正在尝试使用 curl_multi_exec(下面是一个示例:http://www.jaisenmathai.com/articles/php-curl-asynchronous.html),它的第二个参数($still_running)让您知道它何时完成发送和接收。但是,同样,我只想知道我的脚本何时发送完毕。当然,如果我在脚本发送完数据之前退出脚本,Web 服务就不会注册接收请求。

另一种查看方式是检测 PHP 何时空闲,等待服务器响应。

我想实现的是这个对话:

  • PHP:您好,请保存此数据
  • WS:好的,呵呵,让我们考虑一下。
  • PHP:是啊! (去做更重要的事情)
  • WS:好的,我处理完了,这是您的回复……PHP?你去哪儿?我觉得习惯了:(

最佳答案

你可以试试

$url = "http://localhost/server.php";
$nodes = array();
$nodes["A"] = array("data" => mt_rand());   <-------- Random Data 
$nodes["B"] = array("data" => mt_rand());
$nodes["C"] = array("data" => mt_rand());
$nodes["D"] = array("data" => mt_rand());

    echo "<pre>";
$mh = curl_multi_init();
$curl_array = array();
foreach ( $nodes as $i => $data ) {
    $curl_array[$i] = curl_init($url);
    curl_setopt($curl_array[$i], CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_array[$i], CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)');
    curl_setopt($curl_array[$i], CURLOPT_POST, true);
    curl_setopt($curl_array[$i], CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl_array[$i], CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($curl_array[$i], CURLOPT_TIMEOUT, 15);
    curl_multi_add_handle($mh, $curl_array[$i]);
    echo "Please save this data  No : $i ", $data['data'], PHP_EOL;
}

echo PHP_EOL ,PHP_EOL;

$running = NULL;
do {
    usleep(10000);
    curl_multi_exec($mh, $running);
} while ( $running > 0 );
$res = array();
foreach ( $nodes as $i => $url ) {
    $curlErrorCode = curl_errno($curl_array[$i]);
    if ($curlErrorCode === 0) {
        $info = curl_getinfo($curl_array[$i]);
        if ($info['http_code'] == 200) { <------- Connection OK
            echo "Cya! (off to do something more important  No : $i Done", PHP_EOL;
            echo curl_multi_getcontent($curl_array[$i]) , PHP_EOL ;
        }
    }
    curl_multi_remove_handle($mh, $curl_array[$i]);
    curl_close($curl_array[$i]);
}
curl_multi_close($mh);

输出

Please save this data  No : A 1130087324
Please save this data  No : B 1780371600
Please save this data  No : C 764866719
Please save this data  No : D 2042666801


Cya! (off to do something more important  No : A Done
Ok, Im done processing, here is your response... 
    {"data":"1130087324"} PHP? Where did you go? 
    I feel used :(
113
Cya! (off to do something more important  No : B Done
Ok, Im done processing, here is your response... 
    {"data":"1780371600"} PHP? Where did you go? 
    I feel used :(
113
Cya! (off to do something more important  No : C Done
Ok, Im done processing, here is your response... 
    {"data":"764866719"} PHP? Where did you go? 
    I feel used :(
112
Cya! (off to do something more important  No : D Done
Ok, Im done processing, here is your response... 
    {"data":"2042666801"} PHP? Where did you go? 
    I feel used :(
113

简单测试服务器 server.php

echo printf("Ok, Im done processing, here is your response... \n\t%s PHP? Where did you go? \n\tI feel used :(\n", json_encode($_REQUEST));

关于php - 如何判断 curl_multi_exec 何时完成_发送_数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12917491/

相关文章:

php - Laravel 更新创建新表规则而不是更新

php - 在php中访问mysql OUT变量时出错

c - CURLOPT_READFUNCTION 在高层次上做了什么?

linux - 如何在 Windows 和 OS X 上的端口 80 上运行 Vagrant Apache

php - EWS nusoap php请求FindItem操作

c# - 如何拆分和合并此数据流管道?

php - 我想获得特定列值mysql的排名

php - 哪些 PHP 标签始终可用?

JavaScript 异步编程 : promises vs generators

javascript - ES6 : Using generator in asynchronous calls