2017-10-10 5 views
0

나는 사진 파일 ID를 얻고 싶다.json 디코딩 전화 사진 전보 ap

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-> 메시지 -> 사진 [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

될 것이다 하나의 사진을 다른 해상도로 배열하면 가장 최신의 것이므로 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'];