-4

how i can access and callback_query->data why i cant access $update->callback_query->data; here is my code:

$update = file_get_contents('php://input');
$update = json_decode($update, true);
$callbak = $update->callback_query->data;

here is the response:

{
    "ok": true,
    "result": [
        {
            "update_id": 167251858,
            "callback_query": {
                "id": 3952005903777779000,
                "from": {
                    "id": 5215115374,
                    "is_bot": false,
                    "first_name": "Task",
                    "last_name": "Paut",
                    "username": "ma2231",
                    "language_code": "en"
                },
                "message": {
                    "message_id": 157,
                    "from": {
                        "id": 5114398973,
                        "is_bot": true,
                        "first_name": "SYsBot",
                        "username": "taskin221"
                    },
                    "chat": {
                        "id": 5215115374,
                        "first_name": "Task",
                        "last_name": "Payout",
                        "username": "ma2231",
                        "type": "private"
                    },
                    "date": 1648165511,
                    "text": "Please enter pin;",
                    "reply_markup": {
                        "inline_keyboard": [
                            [
                                {
                                    "text": 1,
                                    "callback_data": 1
                                },
                                {
                                    "text": 2,
                                    "callback_data": 2
                                },
                                {
                                    "text": 3,
                                    "callback_data": 3
                                }
                            ],
                            [
                                {
                                    "text": 4,
                                    "callback_data": 4
                                },
                                {
                                    "text": 5,
                                    "callback_data": 5
                                },
                                {
                                    "text": 6,
                                    "callback_data": 6
                                }
                            ],
                            [
                                {
                                    "text": 7,
                                    "callback_data": 7
                                },
                                {
                                    "text": 8,
                                    "callback_data": 8
                                },
                                {
                                    "text": 9,
                                    "callback_data": 9
                                }
                            ],
                            [
                                {
                                    "text": "<",
                                    "callback_data": "<"
                                },
                                {
                                    "text": 0,
                                    "callback_data": 0
                                },
                                {
                                    "text": "OK",
                                    "callback_data": "ok"
                                }
                            ]
                        ]
                    }
                },
                "chat_instance": -5726646197225583000,
                "data": 9
            }
        }
    ]
}
ADyson
  • 57,178
  • 14
  • 51
  • 63
How To
  • 1
  • 2
  • Well, just look at the JSON for a moment. Quite clearly, your top-level data doesn't contain callback_query. So why exactly do you expect `$update->callback_query` to work - what was your logic for writing that code? The top-level data clearly contains two properties: "ok", which is a boolean, and "result", which is an array, and then the object at the first index of that array is what contains callback_query. – ADyson Mar 25 '22 at 10:06
  • Please read [How to extract and access data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-to-extract-and-access-data-from-json-with-php) to learn how to interpret any JSON structure and write equivalent PHP code to access it. Also read https://www.php.net/manual/en/function.json-decode.php and make sure you've understood what all the input parameters do. – ADyson Mar 25 '22 at 10:07
  • you mean the request must be $update->result->callback_query->data ?? ive tried this but no result so i guess to use $update->callback_query->data and no effect – How To Mar 25 '22 at 10:09
  • `you mean the request must be $update->result->callback_query->data`....no, because, as I already said, $result is an **array**. And as I also already said, callback_query is within the _first index_ of that array. Do you know what `[ ... ]` means in JSON? Do you how to access items within an array with PHP? Read the link I gave so you understand the principles. Bookmark the article so you can refer to it another time too. Also look at the answer below by Justinas. – ADyson Mar 25 '22 at 10:10
  • If you find it easier to read the PHP version of the data structure, then use `print_r($update);` after your call to `json_decode` so you can see what the PHP data looks like, and you can see what is an object, what is an indexed array, what is an associative array, what is just a simple variable, etc. – ADyson Mar 25 '22 at 10:12
  • thank you friend im glade for your help this really help – How To Mar 25 '22 at 10:35

1 Answers1

1

Look at documentation:

 json_decode(
    string $json,
    ?bool $associative = null,
    int $depth = 512,
    int $flags = 0
): mixed

and you are passing second argument as true, so it returns array and not object.

And you missing intermediate keys.


So final solution would be:

$update = file_get_contents('php://input');
$update = json_decode($update, true);
$callbak = $update['result'][0]['callback_query']['data'];
Justinas
  • 41,402
  • 5
  • 66
  • 96