/** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. */ protected function setUp() { // Init transient VkPhpSdk $this->transientVkPhpSdk = new TransientVkPhpSdk(); $this->transientVkPhpSdk->setAccessToken(self::ACCESS_TOKEN); $this->transientVkPhpSdk->setUserId(self::USER_ID); // Init transient VkPhpSdk with wrong curl response $this->transientVkPhpSdkWithWrongCurlResponse = new TransientVkPhpSdkWithWrongCurlResponse(); $this->transientVkPhpSdkWithWrongCurlResponse->setAccessToken(self::ACCESS_TOKEN); $this->transientVkPhpSdkWithWrongCurlResponse->setUserId(self::USER_ID); // Init transient VkPhpSdk with wrong VK response $this->transientVkPhpSdkWithWrongVkResponse = new TransientVkPhpSdkWithWrongVkResponse(); $this->transientVkPhpSdkWithWrongVkResponse->setAccessToken(self::ACCESS_TOKEN); $this->transientVkPhpSdkWithWrongVkResponse->setUserId(self::USER_ID); }
/** * Initializes VK API * * @package Easy Digital Downloads - Social Login * @since 1.3.0 */ public function edd_slg_initialize_vk() { //vk.com authentcation code if (isset($_REQUEST['state']) && !empty($_REQUEST['state']) && $_REQUEST['state'] == $_SESSION['vkPhpSdkstate'] && isset($_GET['eddslg']) && $_GET['eddslg'] == 'vk') { // //load vk class $vk = $this->edd_slg_load_vk(); //check vk class is loaded or not if (!$vk) { return false; } // Authentication URL $vk_auth_url = $this->vk->_accessTokenUrl . '?client_id=' . $this->vk->_clientId . '&client_secret=' . $this->vk->_clientSecret . '&code=' . $_REQUEST['code'] . '&redirect_uri=' . $this->vk->_redirectUri; $auth_json = $this->edd_slg_get_data_from_url($vk_auth_url); //json_decode( $this->vk->_authJson ); $auth_json = $this->vk->object_to_array($auth_json); if (!empty($auth_json) && !empty($auth_json['access_token'])) { $vkPhpSdk = new VkPhpSdk(); $vkPhpSdk->setAccessToken($auth_json['access_token']); $vkPhpSdk->setUserId($auth_json['user_id']); // API call - get profile $user_profile_data = $vkPhpSdk->api('getProfiles', array('uids' => $vkPhpSdk->getUserId(), 'fields' => 'uid, first_name, last_name, nickname, screen_name, photo_big, email')); $user_profile_data = isset($user_profile_data['response'][0]) ? $user_profile_data['response'][0] : array(); $user_data_session = EDD()->session->get('edd_slg_vk_user_cache'); //Add email field to array if found email address field if (isset($user_data_session['email']) && !empty($user_data_session['email'])) { $user_profile_data['email'] = $auth_json['email']; } $auth_json = array_merge($auth_json, $user_profile_data); //$_SESSION['edd_slg_vk_user_cache'] = $auth_json; EDD()->session->set('edd_slg_vk_user_cache', $auth_json); } } }
public function loadsuggestions() { if (isset($_SESSION['VKAT']) && isset($_SESSION['VKID']) && isset($_SESSION['B2SESSION'])) { // Init vk.com SDK $vkPhpSdk = new VkPhpSdk(); $vkPhpSdk->setAccessToken($_SESSION['VKAT']); $vkPhpSdk->setUserId($_SESSION['VKID']); usleep(650000); $result = $vkPhpSdk->api('ads.getSuggestions', array('lang' => 'ru', 'section' => 'religions')); $this->load->model('M_vk_religion', 'm_vk_religion'); for ($i = 0; $i < count($result['response']); $i++) { $cnt = $this->jservice->get(array('Action' => 'FindByID', 'Table' => 'vk_religion', 'IDField' => 'vkid', 'IDValue' => $result['response'][$i]['id'])); $crowid = ''; $cinstanceid = '{E987D5B4-87DA-471D-BEB2-1F250951AC50}'; if (count($cnt) > 0) { $crowid = $cnt->rowid; } $data = array('vk_religionid' => $crowid, 'instanceid' => $cinstanceid, 'title' => $result['response'][$i]['name'], 'vkid' => $result['response'][$i]['id']); $vk_rel = $this->m_vk_religion->setRow($data); } usleep(650000); $result = $vkPhpSdk->api('ads.getSuggestions', array('lang' => 'ru', 'section' => 'positions')); $this->load->model('M_vk_position', 'm_vk_position'); for ($i = 0; $i < count($result['response']); $i++) { $cnt = $this->jservice->get(array('Action' => 'FindByID', 'Table' => 'vk_position', 'IDField' => 'vkid', 'IDValue' => $result['response'][$i]['id'])); $crowid = ''; $cinstanceid = '{B3E25A1F-BD2B-48A6-BADE-0802030863F3}'; if (count($cnt) > 0) { $crowid = $cnt->rowid; } $data = array('vk_positionid' => $crowid, 'instanceid' => $cinstanceid, 'title' => $result['response'][$i]['name'], 'vkid' => $result['response'][$i]['id']); $vk_rel = $this->m_vk_position->setRow($data); } usleep(650000); $result = $vkPhpSdk->api('ads.getSuggestions', array('lang' => 'ru', 'section' => 'interest_categories')); $this->load->model('M_vk_interest', 'm_vk_interest'); for ($i = 0; $i < count($result['response']); $i++) { $cnt = $this->jservice->get(array('Action' => 'FindByID', 'Table' => 'vk_interest', 'IDField' => 'vkid', 'IDValue' => $result['response'][$i]['id'])); $crowid = ''; $cinstanceid = '{271C9FA4-F855-40BA-BB43-37094896C18B}'; if (count($cnt) > 0) { $crowid = $cnt->rowid; } $data = array('vk_interestid' => $crowid, 'instanceid' => $cinstanceid, 'title' => $result['response'][$i]['name'], 'vkid' => $result['response'][$i]['id']); $vk_rel = $this->m_vk_interest->setRow($data); } usleep(650000); $result = $vkPhpSdk->api('ads.getSuggestions', array('lang' => 'ru', 'section' => 'user_devices')); $this->load->model('M_vk_device', 'm_vk_device'); for ($i = 0; $i < count($result['response']); $i++) { $cnt = $this->jservice->get(array('Action' => 'FindByID', 'Table' => 'vk_device', 'IDField' => 'vkid', 'IDValue' => $result['response'][$i]['id'])); $crowid = ''; $cinstanceid = '{7EAB742D-8FCA-4273-A8BC-9714BC5322E3}'; if (count($cnt) > 0) { $crowid = $cnt->rowid; } $data = array('vk_deviceid' => $crowid, 'instanceid' => $cinstanceid, 'title' => $result['response'][$i]['name'], 'vkid' => $result['response'][$i]['id']); $vk_dev = $this->m_vk_device->setRow($data); } usleep(650000); $result = $vkPhpSdk->api('ads.getSuggestions', array('lang' => 'ru', 'section' => 'user_os')); $this->load->model('M_vk_os', 'm_vk_os'); for ($i = 0; $i < count($result['response']); $i++) { $cnt = $this->jservice->get(array('Action' => 'FindByID', 'Table' => 'vk_os', 'IDField' => 'vkid', 'IDValue' => $result['response'][$i]['id'])); $crowid = ''; $cinstanceid = '{CD999811-051C-40E6-A946-556F4E355F94}'; if (count($cnt) > 0) { $crowid = $cnt->rowid; } $data = array('vk_osid' => $crowid, 'instanceid' => $cinstanceid, 'title' => $result['response'][$i]['name'], 'vkid' => $result['response'][$i]['id']); $vk_os = $this->m_vk_os->setRow($data); } usleep(650000); $result = $vkPhpSdk->api('ads.getSuggestions', array('lang' => 'ru', 'section' => 'user_browsers')); $this->load->model('M_vk_browser', 'm_vk_browser'); for ($i = 0; $i < count($result['response']); $i++) { $cnt = $this->jservice->get(array('Action' => 'FindByID', 'Table' => 'vk_browser', 'IDField' => 'vkid', 'IDValue' => $result['response'][$i]['id'])); $crowid = ''; $cinstanceid = '{678498A5-EEFE-43A5-8391-A744BBB813E2}'; if (count($cnt) > 0) { $crowid = $cnt->rowid; } $data = array('vk_browserid' => $crowid, 'instanceid' => $cinstanceid, 'title' => $result['response'][$i]['name'], 'vkid' => $result['response'][$i]['id']); $vk_brw = $this->m_vk_browser->setRow($data); } usleep(650000); $result = $vkPhpSdk->api('ads.getSuggestions', array('lang' => 'ru', 'section' => 'interests')); $this->load->model('M_vk_funs', 'm_vk_funs'); // echo count($result['response']).'\r\n'; for ($i = 0; $i < count($result['response']); $i++) { // echo $result['response'][$i].'\r\n'; $cnt = $this->jservice->get(array('Action' => 'FindByID', 'Table' => 'vk_funs', 'IDField' => 'title', 'IDValue' => $result['response'][$i])); $crowid = ''; $cinstanceid = '{7DA638F8-3F1E-4597-B368-0C0B47D596C4}'; if (count($cnt) > 0) { $crowid = $cnt->rowid; } $data = array('vk_funsid' => $crowid, 'instanceid' => $cinstanceid, 'title' => $result['response'][$i]); $vk_fun = $this->m_vk_funs->setRow($data); } print json_encode(array('success' => TRUE, 'msg' => 'OK')); } else { print json_encode(array('success' => FALSE, 'msg' => 'loadsuggesstions - error not logged in')); } }
relogon: // Init OAuth 2.0 proxy $oauth2Proxy = new Oauth2Proxy('5302646', 'LUJf4wzPiglsF5uW8xNe', 'https://oauth.vk.com/access_token', 'https://oauth.vk.com/authorize', 'code', 'http://vas.baminote2.local', 'offline,notify,friends,photos,video,wall,ads'); if ($oauth2Proxy->authorize() === true) { $vkat = $oauth2Proxy->getAccessToken(); $vkid = $oauth2Proxy->getUserId(); $ok = true; } else { echo 'Autorization error'; } } // Try to authorize client if ($ok === true) { // Init vk.com SDK $vkPhpSdk = new VkPhpSdk(); $vkPhpSdk->setAccessToken($vkat); $vkPhpSdk->setUserId($vkid); $_SESSION['VKAT'] = $vkat; // store token in session $_SESSION['VKID'] = $vkid; // store user id in session setcookie("lastvkid", $_SESSION['VKID'], time() + 60 * 60 * 24 * 7, "/", "vas.baminote2.local", false, true); $res = $this->jservice->get(array('Action' => 'Login', 'Email' => $_SESSION['VKID'], 'PasswordHash' => $_SESSION['VKID'], 'ApplicationID' => '')); if (strlen($res) == 38) { $_SESSION['B2SESSION'] = $res; $this->jservice->get(array('Action' => 'StoreToken', 'VKID' => $_SESSION['VKID'], 'VKAT' => $_SESSION['VKAT'])); // store in database for robot actions header("Cache-control: public"); header("Cache-control: max-age=86400"); getPage($_SESSION['VKID']); } else {
<?php require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'VkPhpSdk.php'; require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'Oauth2Proxy.php'; // Init OAuth 2.0 proxy $oauth2Proxy = new Oauth2Proxy('2623235', 'FtoFreQZeezym3DCviid', 'https://oauth.vk.com/access_token', 'https://oauth.vk.com/authorize', 'code', 'http://localhost/vkPhpSdk/example', 'offline,notify,friends,photos,audio,video,wall'); // Try to authorize client if ($oauth2Proxy->authorize() === true) { // Init vk.com SDK $vkPhpSdk = new VkPhpSdk(); $vkPhpSdk->setAccessToken($oauth2Proxy->getAccessToken()); $vkPhpSdk->setUserId($oauth2Proxy->getUserId()); // API call - get profile $result = $vkPhpSdk->api('getProfiles', array('uids' => $vkPhpSdk->getUserId(), 'fields' => 'uid, first_name, last_name, nickname, screen_name, photo_big')); echo 'My profile: <br />'; echo '<pre>'; print_r($result); echo '</pre>'; // API call - wall post $result = $vkPhpSdk->api('wall.post', array('owner_id' => $vkPhpSdk->getUserId(), 'message' => 'Wellcome to vkPhpSdk!')); echo 'Wall post response: <br />'; echo '<pre>'; print_r($result); echo '</pre>'; } else { echo 'Error occurred'; }