json 解码通话照片电报 api

json decode call photo telegram api

我想获取照片文件 ID。

file_get_contents('php://input'):

{
    "update_id": 399206890,
    "message": {
        "message_id": 149,
        "from": {
            "id": 81777999,
            "is_bot": false,
            "first_name": "@goldenguardbot",
            "last_name": "✅",
            "username": "amirntm",
            "language_code": "en-US"
        },
        "chat": {
            "id": 81777999,
            "first_name": "@goldenguardbot",
            "last_name": "✅",
            "username": "amirntm",
            "type": "private"
        },
        "date": 1507643430,
        "photo": [
            {
                "file_id": "AgADBAADpaoxG6lZ6VIeePTlKxaJsl3X-RkABPbjyThHJgF7FLwBAAEC",
                "file_size": 1639,
                "file_path": "photos/file_4.jpg",
                "width": 90,
                "height": 72
            },
            {
                "file_id": "AgADBAADpaoxG6lZ6VIeePTlKxaJsl3X-RkABIrarvPZGVNGFrwBAAEC",
                "file_size": 22230,
                "width": 320,
                "height": 256
            },
            {
                "file_id": "AgADBAADpaoxG6lZ6VIeePTlKxaJsl3X-RkABKFhu79tL5EBF7wBAAEC",
                "file_size": 95422,
                "width": 800,
                "height": 640
            },
            {
                "file_id": "AgADBAADpaoxG6lZ6VIeePTlKxaJsl3X-RkABGzlLqe_Yv0PFbwBAAEC",
                "file_size": 172689,
                "width": 1160,
                "height": 928
            }
        ]
    }
}

我要文件id怎么弄? 例如:

$update->message->photo->file_id;

"photo" 部分是一个数组。 JSON ([]) 中的方括号表示索引数组。花括号 ({}) 表示对象或关联数组,具体取决于您选择如何在 PHP.

中解析它

你没有提到你想要哪个 "file_id",但假设你想要第一个:

$update = json_decode(file_get_contents('php://input'));
echo $update->message->photo[0]->file_id;

你需要将它解码为PHP object/array,并在$update->message->photo中获取照片,这将是一张不同分辨率的照片的数组,最新的总是最大的,所以你可以使用end($photos)来获取它,file_id是对应的文件标识。

代码示例:

$json = file_get_contents('php://input'): 
$update = json_decode($json);
$photos = $update->message->photo;
$photo = end($photo);
$file_id = $photo['file_id'];