array_merge():参数#1不是数组

时间:2017-06-29 06:26:29

标签: php json api curl array-merge

需要你的帮助...在我的array_merge上出错了 这是我的代码:

//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);

    //second
    $url2="https://www.zopim.com/api/v2/chats?page=2";
    $ch2 = curl_init();
    curl_setopt($ch2, CURLOPT_URL, $url2);
    curl_setopt($ch2, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch2, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
    $output2 = curl_exec($ch2);
    $info2 = curl_getinfo($ch2);
    curl_close($ch2);

    $chats2 = json_decode($output2,true);



    $r = [];
    if(is_array($chats1) && is_array($chats2))
    {
        foreach($chats1 as $key => $array)
        {
            $r[$key] = array_merge($chats2[$key], $array);
        }
    }
    else
    {
    echo 'problem with json';
    }
    echo json_encode($r, JSON_UNESCAPED_SLASHES);

我需要使用array_merge()... im结合两个json使用curl授权来调用我的API ..

但是当我尝试运行代码时出现错误: enter image description here

这里的44号错误: enter image description here

1 个答案:

答案 0 :(得分:1)

这意味着您的json_decode失败了。如果字符串没有有效的JSON,它将失败。如果失败,json_decode将返回null或false,因此您必须检查响应是否有效:

$chats1 = json_decode($output1, true);
$chats2 = json_decode($output2, true);

if ($chats1 && $chats2 && is_array($chats1) && is_array($chats2)) {
    // your code goes here
}