skip to Main Content

i can extract user id from JSON response,i get response from telegram bot API, but i can not extract file_id.
here is my code:

 <?php
$response='{"ok":true,"result":[{"update_id":514191839,"message":{"message_id":898,"from":{"id":100000000,"first_name":"Nnn","username":"myid"},"chat":{"id":101150186,"first_name":"Nnn","username":"myid","type":"private"},"date":1487570256,"photo":[{"file_id":"AgADBAAD4KcxG9qfWFEqGZHRzv1IReOGnhkABOym5qINB41gwLIBAAEC","file_size":480,"width":90,"height":44},{"file_id":"AgADBAAD4KcxG9qfWFEqGZHRzv1IReOGnhkABCkXQqPZCWHswbIBAAEC","file_size":2694,"width":273,"height":132}]}}]}';
$arrayUpdate= json_decode($response, true);
foreach ($arrayUpdate['result'] as $key) {

                echo "user id is:".$key['message']['from']['id']."<br />";
                echo "photo file id is:".$key['photo']['file_id'];

}
?>

3

Answers


  1. The field photo is an array of elements. So, for example, if you want to extract the first one, you should use:

    echo "photo file id is:".$key['message']['photo'][0]['file_id'];
    
    Login or Signup to reply.
  2. foreach ($arrayUpdate['result'] as $key) {
    
        echo "user id is:" . $key['message']['from']['id'] . "<br />";
        foreach ($key['message']['photo'] as $photo) {
            echo "photo file id is:" . $photo['file_id'] . "<br />";
        }
    }
    
    Login or Signup to reply.
  3. Try this :

    <?php
       $response='{"ok":true,"result":[{"update_id":514191839,"message":{"message_id":898,"from":{"id":100000000,"first_name":"Nnn","username":"myid"},"chat":{"id":101150186,"first_name":"Nnn","username":"myid","type":"private"},"date":1487570256,"photo":[{"file_id":"AgADBAAD4KcxG9qfWFEqGZHRzv1IReOGnhkABOym5qINB41gwLIBAAEC","file_size":480,"width":90,"height":44},{"file_id":"AgADBAAD4KcxG9qfWFEqGZHRzv1IReOGnhkABCkXQqPZCWHswbIBAAEC","file_size":2694,"width":273,"height":132}]}}]}';
    $arrayUpdate= json_decode($response, true);
    foreach ($arrayUpdate['result'] as $key) {
    
                    echo "user id is:".$key['message']['from']['id']."<br />";
                    foreach ($key['message']['photo'] as $photo) {
                    echo "photo file id is:".$photo['file_id'];
                    }
    
    }
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search