正在获取消息聊天id Telegram


Getting message chat id Telegram

问题

嗨,我想通过Codeigniter获得电报中的消息聊天id。我编程不太好。我认为我没有正确使用数组脚本。

这给了我错误

无法将stdClass类型的对象用作数组C:''OpenServer''domains''localhost''admin''application''controllers''lifeChange.php(第20行)

##PHP##

$chat_id = $update['result'][0]['message']['chat']['id'];

阵列

stdClass Object
(
[ok] => 1
[result] => Array
    (
        [0] => stdClass Object
            (
                [update_id] => 188680055
                [message] => stdClass Object
                    (
                        [message_id] => 9
                        [from] => stdClass Object
                            (
                                [id] => [number id]
                                [first_name] => $ravshan
                                [last_name] => = array(' ');
                                [username] => antiSmoke
                            )
                        [chat] => stdClass Object
                            (
                                [id] => [number id]
                                [first_name] => $ravshan
                                [last_name] => = array(' ');
                                [username] => antiSmoke
                                [type] => private
                            )
                        [date] => 1469289772
                        [text] => /start
                        [entities] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [type] => bot_command
                                        [offset] => 0
                                        [length] => 6
                                    )
                            )
                    )
            )
    )

)

根据您发布的$update变量的内容,以下是如何逐步找到正确的语法:

您写道:

$chat_id = $update['result'][0]['message']['chat']['id'];

但是$update是一个对象(这是第一行stdClass Object),所以要访问result,必须使用:

$chat_id = $update->result;

result是一个数组,因此您确实可以通过索引访问它

$chat_id = $update->result[0];

result[0]是一个对象:

$chat_id = $update->result[0]->message;

message是一个对象:

$chat_id = $update->result[0]->message->chat;

CCD_ 7是一个对象。这是您需要的代码行:

$chat_id = $update->result[0]->message->chat->id;

如果你习惯于操作数组和对象,你将能够非常快速地编写这种代码。您只需要知道要访问的变量的内部结构,var_dump在这方面有很大帮助。

文件:

  • 阵列
  • 类和对象>属性