skip to Main Content

I receive data asynchronously (curl_multi_exec) from JSON.
As a result, how to divide the received data into 2 variables ($response_url and $response_url2)?

I need two variables to continue working with each JSON separately.

$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;

var_dump:

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"
}
}

Thanks!

2

Answers


  1.     $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);
    
    Login or Signup to reply.
  2. Use list function.

    An example :

    $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"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search