Reputation: 41
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'];
}
?>
Upvotes: 0
Views: 69
Reputation: 27192
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'];
}
}
?>
Upvotes: 1
Reputation: 412
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'];
Upvotes: 2
Reputation: 1692
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 />";
}
}
Upvotes: 1