Advertisement
alexjmas

PHP Vk.com API class

Feb 5th, 2013
5,687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.13 KB | None | 0 0
  1. <?php
  2.  
  3. // Предполагается, что сессия уже запущена, если нет - уберите комментарий
  4. // session_start();
  5.  
  6. /**
  7.  * @class VkApi
  8.  * @author Maslakov Alexander <jmas.ukraine@gmail.com>
  9.  */
  10. class VkApi
  11. {
  12.     public $apiKey;
  13.     public $appId;
  14.     public $login;
  15.     public $password;
  16.     public $authRedirectUrl;
  17.     public $apiUrl = 'https://api.vk.com/method/';
  18.     public $v = '2.0';
  19.     private $_sid;
  20.    
  21.    
  22.     public function __construct($options)
  23.     {
  24.         foreach ($options as $key=>$value) {
  25.             $this->{$key} = $value;
  26.         }
  27.        
  28.         $this->_auth();
  29.     }
  30.    
  31.    
  32.     private function _auth()
  33.     {
  34.         $token = file_get_contents('./Cookies.txt');
  35.        
  36.         if (isset($_GET['code'])) {
  37.             $url  = 'https://oauth.vk.com/access_token?client_id='.$this->appId.'&client_secret='. $this->apiKey .'&code=' . $_GET['code'] . '&redirect_uri=' . $this->authRedirectUrl;
  38.                        
  39.             $ch = curl_init();
  40.             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  41.             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  42.             curl_setopt($ch, CURLOPT_HEADER, false);
  43.             curl_setopt($ch, CURLOPT_URL, $url);
  44.             curl_setopt($ch, CURLOPT_REFERER, $url);
  45.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  46.            
  47.             $responce = curl_exec($ch);
  48.            
  49.             curl_close($ch);
  50.    
  51.             $responce = json_decode($responce);
  52.        
  53.             if (isset($responce->access_token)) {
  54.                  file_put_contents('./Cookies.txt', $responce->access_token);
  55.                  $token = $responce->access_token;
  56.             } else {
  57.                 throw new Exception('VK API error.');
  58.                 //var_dump($responce);exit;
  59.             }
  60.         }
  61.        
  62.         if (empty($token)) {
  63.             $url = "https://oauth.vk.com/authorize?client_id="
  64.                    . $this->appId . "&redirect_uri=http://jmas.koding.com/agregator/vk.php&display=page&response_type=code&scope=video,offline";
  65.  
  66.             header('Location: ' . $url);
  67.             exit;
  68.         }
  69.        
  70.         $this->_accessToken = $token;
  71.     }
  72.  
  73.     public function get($method, $params=false)
  74.     {
  75.         if (! $params) $params = array();
  76.  
  77.         $params['format'] = 'json';
  78.         $url = $this->apiUrl . $method;
  79.         $params['access_token'] = $this->_accessToken;
  80.        
  81.         ksort($params);
  82.        
  83.         $sig = '';
  84.        
  85.         foreach ($params as $k=>$v) {
  86.             $sig .= $k.'='.$v;
  87.         }
  88.        
  89.         $sig .= $this->apiKey;
  90.        
  91.         $params['sig'] = md5($sig);
  92.        
  93.         $query = $url . '?' . $this->_params($params);
  94.        
  95.         $ch = curl_init();
  96.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  97.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  98.        
  99.         curl_setopt($ch, CURLOPT_HEADER, false);
  100.         curl_setopt($ch, CURLOPT_URL, $query);
  101.         curl_setopt($ch, CURLOPT_REFERER, $query);
  102.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  103.         curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)");
  104.         $res = curl_exec($ch);
  105.         curl_close($ch);
  106.  
  107.         return json_decode($res, true);
  108.     }
  109.    
  110.  
  111.     public function getLikesCountByUrl($pageUrl)
  112.     {
  113.         if (parse_url($pageUrl, PHP_URL_HOST) != parse_url($this->authRedirectUrl, PHP_URL_HOST)) {
  114.             throw new Exception('Page URL not valid!');
  115.         }
  116.        
  117.         $request = $this->get('likes.getList', array(
  118.             'type' => 'sitepage',
  119.             'owner_id' => $this->appId,
  120.             'page_url' => $pageUrl,
  121.         ));
  122.        
  123.         $this->_responce($request);
  124.     }
  125.  
  126.  
  127.     public function searchVideo($q, $offset)
  128.     {        
  129.         $request = $this->get('video.search', array(
  130.             'q' => $q,
  131.             'offset' => $offset,
  132.         ));
  133.  
  134.         return $this->_responce($request);
  135.     }
  136.    
  137.    
  138.     public function getWall($owner_id, $offset = 0, $count = 100, $filter = 'owner')
  139.     {
  140.         $request = $this->get('wall.get', array(
  141.             'owner_id' => '-' . $owner_id,
  142.             'offset' => $offset,
  143.             'count' => $count,
  144.             'filter' => $filter,
  145.         ));
  146.  
  147.         return $this->_responce($request);
  148.     }
  149.    
  150.  
  151.  
  152.     private function _params($params) {
  153.         $pice = array();
  154.         foreach($params as $k=>$v) {
  155.             $pice[] = $k.'='.urlencode($v);
  156.         }
  157.         return implode('&',$pice);
  158.     }
  159.  
  160.  
  161.     private function _responce($request)
  162.     {
  163.         if (isset($request['response'])) {
  164.             return $request['response'];
  165.         } else if (isset($request['error'])) {
  166.             throw new Exception($request['error']['error_msg']);
  167.         }
  168.        
  169.         return null;
  170.     }  
  171. }
  172.  
  173.  
  174. // Пример использования
  175. $vk = new VkApi(array(
  176.     'apiKey' => '<API_KEY>',
  177.     'appId' => '<APP_ID>',
  178.     'login' => '<LOGIN>',
  179.     'password' => '<PASSWORD>',
  180.     'authRedirectUrl' => '<URL_OF_THIS_PAGE>',
  181. ));
  182.  
  183. echo '<pre>';
  184. var_dump($vk->getWall($_GET['owner_id']));
  185. echo '</pre>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement