在解码的json中访问数组值

时间:2019-08-09 09:46:00

标签: php arrays json access livechat

我们使用我们网站上的livechatinc.com扩展名,希望在每次聊天结束后自动将聊天记录发送给代理。

您看到他们的网站上有一个我们已实施的示例:

https://www.livechatinc.com/help/how-to-get-chat-transcripts/

这仅适用于硬编码的电子邮件地址。

在这种情况下,我们需要使用从API接收到的JSON,将电子邮件发送到代理的电子邮件地址,该地址为密钥[agent_id],其值为email@email.bhma.co.uk。

我们尝试了以下方法

$agent = $data->chat->messages[0]->agent_id;
$agent = $data->chat->messages->agent_id;

然后

mail($agent, 'Chat transcript', print_r($chat));

但是,尝试访问agent_id时存在问题。

似乎直到$ data->聊天我们才能获取一些数据,然后才有可能?

    stdClass Object
(
    [event_type] => chat_ended
    [event_unique_id] => 123
    [token] => 123
    [license_id] => 123
    [lc_version] => 1
    [chat] => stdClass Object
        (
            [id] => PVDXP0E1Q9
            [started_timestamp] => 1565262189
            [ended_timestamp] => 1565262198
            [url] => https://www..co.uk/
            [referer] => 
            [messages] => Array
                (
                    [0] => stdClass Object
                        (
                            [user_type] => agent
                            [author_name] => Bob
                            [agent_id] => email@email.co.uk
                            [text] => Hello . How may I help you?
                            [json] => 
                            [timestamp] => 1565262189
                        )

                )

            [attachments] => Array
                (
                )

            [events] => Array
                (
                    [0] => stdClass Object
                        (
                            [user_type] => 
                            [text] => archived the chat
                            [timestamp] => 1565262198
                            [type] => 
                        )

                )

            [agents] => Array
                (
                    [0] => stdClass Object
                        (
                            [name] => Bob
                            [login] => email@email.co.uk
                        )

                )

            [tags] => Array
                (
                )

            [groups] => Array
                (
                    [0] => 0
                )

        )


)

因此,我们需要访问[agent_id]值,然后将其分配给用于向其发送电子邮件的变量。

0 个答案:

没有答案