2017-10-10 286 views
0

我想獲取照片文件ID。json解碼通話照片電報api

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; 
+0

'$最新情況:> MESSAGE->相片[0] - > file_id' – WillardSolutions

+0

[json_decode()](http://php.net/manual/en/function。 json-decode.php) – RiggsFolly

+0

如果你不理解json字符串表示法,那麼總是通過執行'$ t = json_decode($ your_json_string);的print_r($噸);' – RiggsFolly

回答

0

「照片」部分是一個數組。 JSON中的方括號([])表示索引數組。大括號({})表示一個對象或關聯數組,取決於您如何選擇使用PHP解析它。

你不提你想要的「的file_id」,但假設你想第一:

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

你需要將其解碼到PHP對象/數組,並在$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'];