-
-
Save gigorok/4578126 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Vkapi { | |
protected $_access_token = ''; | |
public static function factory () | |
{ | |
$class = get_class(); | |
return new $class; | |
} | |
public function method ($method, array $params = array(), $as_array = FALSE) | |
{ | |
if ( ! $params) | |
{ | |
$params = array(); | |
} | |
$url = 'https://api.vkontakte.ru/method/'.$method.'?'; | |
if (count($params) > 0) | |
{ | |
foreach ($params AS $key => $value) | |
{ | |
$url .= $key.'='.$value.'&'; | |
} | |
} | |
$url .= 'access_token='.$this->_access_token; | |
$content = file_get_contents($url); | |
$result = json_decode($content, (bool) $as_array); | |
$result = ( (bool) $as_array) ? $result['response'] : $result->response; | |
if ($result === NULL) | |
{ | |
throw new Exception('Some error with VK API'); | |
} | |
return $result; | |
} | |
protected $_client_id = 0; | |
protected $_client_secret = ''; | |
public function auth () | |
{ | |
print file_get_contents('http://api.vkontakte.ru/oauth/authorize?client_id='.$this->_client_id.'&scope=offline,video,wall&redirect_uri=http://api.vkontakte.ru/blank.html&display=page&response_type=token'); | |
} | |
public function access_token ($code) | |
{ | |
print file_get_contents('https://api.vkontakte.ru/oauth/access_token?client_id='.$this->_client_id.'&client_secret='.$this->_client_secret.'&code='.$code); | |
} | |
} | |
// Получаем access_token, один раз, не самое удачное решение — ибо мы выводим html от вконтакта. Но тем не менее, заполнив форму входа ее можно отправить и получить нужный access_token. Так как при запроса запрашиваются права оффлайн-доступа, этот токен будет работать вечно, до смены пароля аккаунта владельца. Дальше его необходимо прописать в свойство в классе. А так же не забыть, ниже в классе, указать client_id — id приложения и client_secret — защищенный ключ приложения. | |
print VKApi::factory()->auth(); | |
exit; // убрать, когда этот этап пройден | |
// Раньше еще нужно было использовать метод Vkapi::factory()->access_token(), но сейчас почему-то уже не нужно. На всякий случай оставил его в коде. | |
// Дальше используем API как нам хочется | |
VkApi::factory()->method('wall.post', array( | |
'owner_id' => '%id%', // id страницы, куда отправить. Если паблик или группа - добавить минус впереди. | |
'message' => '%message%', | |
'from_group' => 1 | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment