繁体   English   中英

如何获取执行curl_multi_exec的结果?

[英]How to get the result of executing curl_multi_exec?

我从 JSON 异步接收数据 ( curl_multi_exec )。 因此,如何将接收到的数据分成 2 个变量( $response_url$response_url2 )?

我需要两个变量来继续分别处理每个 JSON。

$urls = [
"https://rssbot.ru/1.json",
"https://rssbot.ru/2.json"
];



$mh = curl_multi_init();     
$allResponse = [];

foreach($urls as $k => $url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_multi_add_handle($mh, $ch);
$allResponse[$k] = $ch;
}

$running = null;

do {
curl_multi_exec($mh, $running);
} while($running > 0);

foreach($allResponse as $id => $ch) {

$response = curl_multi_getcontent($ch);

curl_multi_remove_handle($mh, $ch);


    $response = (json_decode($response));
    var_dump($response);

}

curl_multi_close($mh);


echo $response_url;

echo $response_url2;

变量转储:

array(2) {
[0]=>
object(stdClass)#1 (3) {
["symbol"]=>
string(7) "XRPBUSD"
["price"]=>
string(6) "0.3400"
["time"]=>
int(1671427537235)
}
[1]=>
object(stdClass)#2 (3) {
["symbol"]=>
string(7) "MKRUSDT"
["price"]=>
string(6) "542.60"
["time"]=>
int(1671427559567)
}
}
array(3) {
[0]=>
object(stdClass)#2 (2) {
["symbol"]=>
string(6) "ETHBTC"
["price"]=>
string(10) "0.07081400"
}
[1]=>
object(stdClass)#1 (2) {
["symbol"]=>
string(6) "LTCBTC"
["price"]=>
string(10) "0.00377700"
}
[2]=>
object(stdClass)#3 (2) {
["symbol"]=>
string(6) "BNBBTC"
["price"]=>
string(10) "0.01482300"
}
}

谢谢!

    $results = []; 
    $prev_running = $running = null;
    do {
        curl_multi_exec($mh, $running);
        if ($running != $prev_running) {
            $info = curl_multi_info_read($mh);
            if (is_array($info) && ($ch = $info['handle'])) {

                $content = curl_multi_getcontent($ch);

                $url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

                $results[$url] = ['content' => $content, 'status' => $info['result'], 'status_text' => curl_error($ch)];
                
            }
            $prev_running = $running;
        }
    } while ($running > 0);

使用list功能。

一个例子:

$urls = [
"https://rssbot.ru/1.json",
"https://rssbot.ru/2.json"
];


list($a, $b) = $urls;

// $a : string(24) "https://rssbot.ru/1.json"
// $b : string(24) "https://rssbot.ru/2.json"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM