
<!doctype html> <html> <head> <meta charset="utf-8"> <title> </title> <link rel="stylesheet" href="style.css" /> <script src="script.js"></script> </head> <body> <section id="serverApi"> API</section> <section id="clientApi"> API</section> </body> </html> VK.init({appid: YOUR_APP_ID}) on the window.vkAsyncInit event, after which you can call the VK.Auth.login(authInfo, YOUR_APP_PERMISSIONS) function VK.Auth.login(authInfo, YOUR_APP_PERMISSIONS) , which will execute user authorization. var vk = { data: {}, api: "//vk.com/js/api/openapi.js", appID: YOUR_APP_ID, appPermissions: YOUR_APP_PERMISSIONS, init: function(){ $.js(vk.api); window.vkAsyncInit = function(){ VK.init({apiId: vk.appID}); load(); } function load(){ VK.Auth.login(authInfo, vk.appPermissions); function authInfo(response){ if(response.session){ // vk.data.user = response.session.user; vk.getFriends(); }else alert(" !"); } } }, getFriends: function(){ VK.Api.call('friends.get', {fields: ['uid', 'first_name', 'last_name'], order: 'name'}, function(r){ if(r.response){ r = r.response; var ol = $('#clientApi').add('ol'); for(var i = 0; i < r.length; ++i){ var li = ol.add('li').html(r[i].first_name+' '+r[i].last_name+' ('+r[i].uid+')') } }else alert(" "); }) } } $.ready(vk.init); $ used here are not jQuery . However, their purpose may not be intuitive, so I will tell you what this or that function does. Their independent implementation should not be difficult, just like putting all the code on jQuery .$.ready(function) - This is the handler for DOMContentLoaded ;$.js(text) - asynchronously loads the javascript file;$(element) - returns a wrapper over the DOM node element ;$(element).add(node) - creates a new child node for an element node node and returns a wrapper over it;$(element).html(string) - wrapper for element.innerHTML = string .VK.Auth.login() method is VK.Auth.login() , which shows a pop-up window authInfo function authInfo called after the user's consent or disagreement, and, in case of successful authorization, requests a list of friends.
iframe tag is still in the standard HTML format, and it is perfectly suited for our task. <?php require_once('vk.php'); session_start(); function getPostStr($data, $default = false){ // - $_POST[$data] if(!isset($_POST[$data])) return $default; $data = $_POST[$data]; $data = htmlspecialchars(strip_tags(trim($data))); return ($data != "" ? $data : $default); } function error($code, $text, $params = Array()){ $result = Array('error' => Array('code' => $code, 'message' => $text)); if(count($params) > 0) foreach($params as $key => $value) $result['error'][$key] = $value; die(json_encode($result)); } $vkConf = Array( 'appID' => YOUR_APP_ID, 'apiSecret' => YOUR_API_SECRET, 'callbackUrl' => YOUR_DOMAIN . '/auth.php', 'apiSettings' => YOUR_APP_PERMISSIONS ); $vk = (isset($_SESSION['accessToken'])) ? new VK($vkConf['appID'], $vkConf['apiSecret'], $_SESSION['accessToken']) : null; function userIn($vk, $vkConf){ // unset($_SESSION['accessToken']); $vk = new VK($vkConf['appID'], $vkConf['apiSecret']); $authorizeUrl = $vk -> getAuthorizeURL($vkConf['apiSettings'], $vkConf['callbackUrl']); error(-1, " !", Array('url' => $authorizeUrl)); } function getFriends($fields, $order, $vk){ // $userFriends = $vk -> api('friends.get', array('fields' => $fields, 'order' => $order)); $result = Array(); foreach($userFriends['response'] as $key => $value){ $result[] = Array('firstName' => $value['first_name'], 'lastName' => $value['last_name'], 'uid' => $value['uid']); } echo json_encode($result); } $method = strtolower($api -> getStr("method")); switch($method){ case "user.in" : userIn($vk, $vkConf); break; case "friends.get" : getFriends(getPostStr("fields"), getPostStr("order"), $vk); break; default: Api::error(0, " Api"); } ?> method contains the name of the method we want to perform on the server.user.in when our main page is ready, to which the server will respond with an error code of -1 , which we will catch and process. function Api(method, params, callback){ if(!params) params = {}; $.ajax($.copy({method: method}, params), 'server.php', function(r){ if(r.error){ switch(r.error.code){ case -1: { // notAuth var div = $(document.body).add('div').id('authPopup'), iframe = div.add('iframe').attr({src: r.error.url}); div.add('p').html(' , - . , , ').add('a').attr({href: r.error.url}).html(' '); $.splash.open({onclose:function(){ div.remove() }}); } break; default: alert(r.error.code+': '+r.error.message); } }else if(callback) callback(r); }); } $.ready(function(){ Api('user.in'); }); $ function:$.copy(object1, object2) - copies all object2 parameters to object2 ;$.ajax(text, addr, callback) - sends a POST request to addr , passing a text message. It accepts the answer in the form of JSON and transmits it to the callback function;$(element).id(elemID) - wrapper for element.id = elemID ;$(element).attr(object) - sets the attributes of the element element tag. object keys - attribute names, key values ​​will be set as attribute values;$(element).remove() - removes element ;$.splash.open(params) - $.splash.open(params) browser window with a translucent dark block. The params.onclose parameter is a function that will be called upon $.splash.close() . <?php require_once('vk.php'); session_start(); $vkConf = Array( 'appID' => YOUR_APP_ID, 'apiSecret' => YOUR_API_SECRET, 'callbackUrl' => YOUR_DOMAIN . '/auth.php', ); if(isset($_REQUEST['code'])){ $vk = new VK($vkConf['appID'], $vkConf['apiSecret']); $accessToken = $vk -> getAccessToken($_REQUEST['code'], $vkConf['callbackUrl']); $_SESSION['accessToken'] = $accessToken['access_token']; }else die('Auth failed'); ?> <!doctype html> <html> <head> <meta charset="utf-8" /> <title> </title> <script> $.ready(function(){ if(window.top){ window.top.vk.getFriends(); window.top.$.splash.close(); } }) </script> </head> <body> <h4> !</h4> <p> <a href="YOUR_DOMAIN"> </a>, </p> </body> </html> 
YOUR_DOMAIN page from the confirmation page, if it is not open in the frame, and improve the userIn() method so that if there is an access_token session, its validity is checked and, if the key is valid, it is used instead of the new authorization .Source: https://habr.com/ru/post/188102/
All Articles