Hormold

VKAPI + oAuth Support

Jan 28th, 2012
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * VKAPI class for vk.com social network
  5.  *
  6.  * @package server API methods (+oauth support)
  7.  * @link http://vk.com/developers.php
  8.  * @autor Oleg Illarionov (oauth support - Nikita A.)
  9.  * @version 1.1
  10.  */
  11.  
  12. class vkapi {
  13.     var $api_secret;
  14.     var $app_id;
  15.     var $api_url;
  16.    
  17.     function vkapi($app_id, $api_secret, $oauth_token = '', $api_url = 'api.vk.com/api.php') {
  18.         $this->app_id = $app_id;
  19.         $this->token = $oauth_token;
  20.         $this->api_secret = $api_secret;
  21.         $this->oauth_url = "https://api.vk.com/";
  22.         if (!strstr($api_url, 'http://')) $api_url = 'http://'.$api_url;
  23.         $this->api_url = $api_url;
  24.     }
  25.    
  26.     function api($method,$params=false,$oauth=false) {
  27.         if($oauth==false){
  28.             if (!$params) $params = array();
  29.             $params['api_id'] = $this->app_id;
  30.             $params['v'] = '3.0';
  31.             $params['method'] = $method;
  32.             $params['timestamp'] = time();
  33.             $params['format'] = 'json';
  34.             $params['random'] = rand(0,10000);
  35.             ksort($params);
  36.             $sig = '';
  37.             foreach($params as $k=>$v) {
  38.                 $sig .= $k.'='.$v;
  39.             }
  40.             $sig .= $this->api_secret;
  41.             $params['sig'] = md5($sig);
  42.             $query = $this->api_url.'?'.$this->params($params);
  43.             $res = file_get_contents($query);
  44.             return json_decode($res, true);
  45.         }else{
  46.             $url="/method/".$method."?".$this->params($params)."&access_token=".$this->token;
  47.             $res = $this->ssl($url);
  48.             return json_decode($res, true);
  49.         }
  50.     }
  51.  
  52.     function token($code){
  53.         $url = $this->ssl('/oauth/access_token?client_id='.$this->app_id.'&code='.$code.'&client_secret='.$this->api_secret);
  54.         $data = json_decode($url, true);
  55.         return $data;
  56.     }
  57.    
  58.     function params($params) {
  59.         $pice = array();
  60.         foreach($params as $k=>$v) {
  61.             $pice[] = $k.'='.urlencode($v);
  62.         }
  63.         return implode('&',$pice);
  64.     }
  65.  
  66.     function ssl($req){
  67.         $ch = curl_init($this->oauth_url.$req);
  68.         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  69.         curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
  70.         curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
  71.         curl_exec ($ch);
  72.         return curl_multi_getcontent ($ch);
  73.         curl_close ($ch);
  74.        
  75.     }
  76. }
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment