[ Новые сообщения · Участники · Правила форума · Поиск · RSS ]
  • Страница 1 из 1
  • 1
Форум » Программирование на PHP » Пользовательские функции » VK Api class
VK Api class
AgManiXДата: Суббота, 03.12.2011, 19:01 | Сообщение # 1
Гл. администратор
Группа: Администраторы
Сообщений: 29
Репутация: 1
Статус: Offline
Автора уже не помню.
Code

class vkapi {
   
         function __construct() {
                 if (!function_exists('json_decode')) {
                         $this->error(1);
                 }
         }
   
         private $app_id;
         private $secret;
         private $token;
   
         private $errors = array(
                  => "No JSON support! To use this class install JSON addon.",
                  => "Incorrect APP ID format! It must be numeric and contain at least 1 character.",
                  => "Incorrect SECRET format! It must contain at least 1 character.",
                  => "Incorrect APP ID or SECRET!",
                  => "Incorrect METHOD name, during query!",
                  => "Incorrect PARAMS array, during query!",
                  => "Authorize first!"
         );
         
         function auth($app_id, $secret) {
   
                 if (intval($app_id)>0 && strlen($secret)>0) {
                         $this->app_id = $app_id;
                         $this->secret = $secret;
                         $url = 'https://api.vkontakte.ru/oauth/access_token/?client_id='.$this->app_id.'&client_secret='.$this->secret.'&grant_type=client_credentials';
                         $ch = curl_init($url);
                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                         $answer = curl_exec($ch);
                         curl_close($ch);
                         $answer = json_decode($answer, true);
                         $this->token = $answer['access_token'];
                         if (strlen($this->token<1)) {
                    $this->error(4);
                         }
                 } else {
                         if (intval($app_id)<1) {
                    $this->error(2);
                         }
                         if (strlen($secret)<1) {
                    $this->error(3);
                         }
                 }
   
         }
         
         function query($method, $params) {
                 if (strlen($this->token<1)) {
                         $this->error(7);
                 }
                 $url = 'https://api.vkontakte.ru/method/';
                 if (strlen($method)>0 && count($params)>0) {
                         $url .= $method.'?';
                         while (list($key, $var) = each($params)) {
                    $url .= "$key=$var&";
                         }
                         $url .= 'access_token='.$this->token;
                         $ch = curl_init($url);
                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                         $answer = curl_exec($ch);
                         curl_close($ch);
                         $answer = json_decode($answer, true);
                         return $answer;
                 } else {
                         if (strlen($method)<1) {
                    $this->error(5);
                         }
                         if (count($params)<1) {
                    $this->error(6);
                         }
                 }
         }
         
         function rand() {
         return rand(10000,99999);
         }
         
         private function error($error_num) {
                 die("<b>VK API Class error #$error_num:</b> ".$this->errors[$error_num]);
         }
         
}
?>

HowTo:
Code

<?
   
require_once 'vkapi.class.php';
   
$appid = 1234567;
$secret = 'app_secretkey';
   
$vk = new vkapi;
$vk->auth($appid, $secret);
   
$method = 'secure.setCounter';
$params = array(
'timestamp' => time(),
'random'    => $vk->rand(),
'uid'       => 1,
'counter'   => 123
);
   
$res=$vk->query($method, $params);
print_r($res);
   
?>


Мой Блог
 
Форум » Программирование на PHP » Пользовательские функции » VK Api class
  • Страница 1 из 1
  • 1
Поиск: