使用php循环卷曲数据

时间:2017-07-05 05:39:53

标签: php json loops curl

需要你的帮助......如何在循环中转换它:

//first
    $url1="https://www.zopim.com/api/v2/chats";
    $ch1 = curl_init();
    curl_setopt($ch1, CURLOPT_URL, $url1);
    curl_setopt($ch1, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch1, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
    $output1 = curl_exec($ch1);
    $info1 = curl_getinfo($ch1);
    curl_close($ch1);

    $chats1 = json_decode($output1,true);

例如,有一个特定的数字计数循环,如4个或更多数字...... 然后我想获得所有$chats ...并合并..

像这样:

$merge_array = array_merge_recursive($chats1, $chats2, $chats3, $chats4);
希望你能帮助我,谢谢...

1 个答案:

答案 0 :(得分:1)

尝试这样做:

$chats = [];
for($i=1; $i<= $n; $i++){
    $url1="https://www.zopim.com/api/v2/chats";
    $ch1 = curl_init();
    curl_setopt($ch1, CURLOPT_URL, $url1);
    curl_setopt($ch1, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch1, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
    $output1 = curl_exec($ch1);
    $info1 = curl_getinfo($ch1);
    curl_close($ch1);

    $chats[] = json_decode($output1,true);
}
$merge_array = call_user_func_array('array_merge_recursive', $chats);
echo json_encode($merge_array);

希望这就是你所需要的。