2017-10-10 5 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

'$ update-> 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($ t); ' – RiggsFolly

答えて

0

「写真」セクションは配列です。 JSONの角括弧([])は、インデックス付き配列を示しています。中括弧({})は、PHPで解析する方法に応じて、オブジェクトまたは連想配列を示します。

あなたがたい「のfile_id」に言及、しかし、あなたが最初に必要と想定していない:

あなたはPHPオブジェクト/配列にそれをデコードし、 $update->message->photoで写真を取得する必要があり
$update = json_decode(file_get_contents('php://input')); 
echo $update->message->photo[0]->file_id; 
0

、されること1つの写真を異なる解像度で並べると、最新のものが常に最大になるので、end($photos)を使用して取得することができ、file_idは対応するファイルIDです。

コード例:

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