M
M
MORGEN666NN2021-09-21 12:11:40
PHP
MORGEN666NN, 2021-09-21 12:11:40

How to extract the access_token array to make authorization through VK in php?

I have a problem that I can't extract the array that appears when following the link
example: https://oauth.vk.com/access_token?client_id=794994...
how can I extract it into a php variable?

code:
<?php
$client_id = "7949945";
$client_secret = "wrmYrwp41Ajf8c7tWvti";
$redirect_uri = " https://onetapped.ru ";

$auth = " https://oauth.vk.com/authorize?client_id=".$client... ";

$access_token = " https://oauth.vk.com/access_token?client_id=".$cli. .. '];

if(!$_GET['code']) {
echo " Login via VK ";

echo " access_token ";
}
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Derepko, 2021-09-21
@uDenX

https://www.php.net/manual/en/function.json-decode.php

T
T3R3AND, 2021-09-22
@T3R3AND

<?php

$app = array(
    'client_id'     => '',
    'client_secret' => '',
    'redirect_uri'  => 'http://' . $_SERVER['HTTP_HOST'] . '/ИМЯ_ФАЙЛА.php'
);

if (!isset($_GET['code']))
    echo '<a href="https://oauth.vk.com/authorize?client_id=' . $app['client_id'] . '&redirect_uri=' . urlencode($app['redirect_uri']) . '&response_type=code">Войти через ВКонтакте</a>';
else {
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://oauth.vk.com/access_token?client_id=' . $app['client_id'] . '&client_secret=' . $app['client_secret'] . '&code=' . $_GET['code'] . '&redirect_uri=' . urlencode($app['redirect_uri']),
        CURLOPT_HEADER => false,
        CURLOPT_RETURNTRANSFER => true
    ));
    $data = json_decode(curl_exec($curl));
    curl_close($curl);
    if (isset($data->access_token)) {
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://api.vk.com/method/users.get?uids=' . $data->user_id . '&fields=uid,first_name,last_name,screen_name,sex,bdate,photo_big&access_token=' . $data->access_token . '&v=5.131',
            CURLOPT_HEADER => false,
            CURLOPT_RETURNTRANSFER => true
        ));
        $data = json_decode(curl_exec($curl));
        curl_close($curl);
        if (isset($data->response[0]->id))
            foreach ($data->response[0] as $key => $value)
                echo $key . ': ' . $value . '<br/>';
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question