M
M
meqy1qub2019-10-05 01:44:41
PHP
meqy1qub, 2019-10-05 01:44:41

How it is possible to implement check on the administrator?

Help to implement the function, or point out my mistakes.

function is_admin($peer_id, $from_id){ // Проверка на админа v1.0
        $param = array(
            'peer_id' => $peer_id,
      'access_token' => $this->token,
            'v' => '5.90'
        );

        $params = http_build_query($param);

        $resp = json_decode(file_get_contents("https://api.vk.com/method/messages.getConversationMembers?$params"));

        foreach($resp['response']['items'] as $key){
            if ($key['member_id'] == $from_id){
                if($key['admin_ids'] == $from_id){
                return 'admin';
                }elseif($key['owner_id'] == $from_id){
                    return 'owner';
                }elseif($key['admin_ids'] != $from_id){
                    return 'false';
                }
            }
        }
    }

p.s. i am new to php

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
ThunderCat, 2019-10-05
@meqy1qub

1) json_decode() will return an object by default, to get an array you need to add true as the second parameter.
2) The function:
a) is called is_admin(), but in fact it defines anything.
b) It returns not yes/no (which is supposed to), but 3 different values
​​c) The third value is false - it is written without quotes, it is a boolean data type.

p.s. i am new to php
The general principles that are violated here apply to any language, not only PHP.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question