L
L
LebedevFF2019-09-27 16:22:59
PHP
LebedevFF, 2019-09-27 16:22:59

How to handle error from vk api?

function ShowPageUser($id = Null){
    $vk = new \VK\Client\VKApiClient();
    $vk_token = ********;
    if ($id == Null) {
        $id = 1;
        $response = $vk->users()->get($vk_token, array(
            'user_ids' => array($id),
            'fields' => array('photo')
        ));
        return json_encode($response[0]);
    } else {
        $response = $vk->users()->get($vk_token, array(
            'user_ids' => array($id),
            'fields' => array('photo_id, counters, verified, sex, bdate, city, country, home_town, has_photo, photo_50, photo_200, photo_400_orig, photo_max_orig, online, domain, has_mobile, contacts, site, education, universities, schools, status, last_seen, followers_count, common_count, occupation, nickname, relatives, relation, personal, connections, exports, activities, interests, music, movies, tv, books, games, about, quotes, can_post, can_see_all_posts, can_see_audio, can_write_private_message, can_send_friend_request, is_favorite, is_hidden_from_feed, timezone, screen_name, maiden_name, crop_photo, is_friend, friend_status, career, military, blacklisted, blacklisted_by_me, can_be_invited_group')
        ));
        if(!empty($response[0])){
            $result = $response[0];
        }else{
            if(!empty(json_decode($response)->error->error_code)) {
                $result = array('error' => 'true');
            }
        }
        return json_encode($result);
    }
}
The method returns nothing and crashes if there is no user... Example ShowPageUser('dv20')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Derepko, 2019-09-29
@xEpozZ

I think you need to fix a piece of code:

if(!empty($response[0])){
    $result = $response[0];
}else{
    if(!empty(json_decode($response)->error->error_code)) {
        $result = array('error' => 'true');
    }
}

on the
if(!empty($response[0])){
    $result = $response[0];
}else{
    $result = $response;
    if(!empty(json_decode($response)->error->error_code)) {
        $result = array('error' => 'true');
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question