0

i use a php post to get the number of users in the telegram group

<?php
$post = array(
'chat_id'=>$chat_id);
$ch = curl_init("https://api.telegram.org/bot$tokken/getChatMembersCount?");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING,"");
header('Content-Type: text/html');
$posres = curl_exec($ch);
$data1 = json_encode($posres);
echo $data1;
?>

the response are array :

{"ok":true,"result":3}

how can i access the result value i tried echo $data1[19]; this only give the index position value thank you

How To
  • 1
  • 2
  • 1
    Why do you think there would be an item in `$data1[19]`, the result just has a count of the users and nothing more (OK except for the `ok` value). – Nigel Ren Mar 20 '22 at 08:44
  • what if the response are {"ok":true,"result":33} then i get the same result 3 – How To Mar 20 '22 at 08:45

2 Answers2

0

The response is JSON, so you need to use json_decode() to convert it to a PHP array or object.

$data1 = json_decode($posres);
echo $data1->result;
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Just in case Your response is array of object

echo $data1['19']->result; this should give 3 according to your question

terinao
  • 491
  • 4
  • 15