Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * VKAPI class for vk.com social network
- *
- * @package server API methods (+oauth support)
- * @link http://vk.com/developers.php
- * @autor Oleg Illarionov (oauth support - Nikita A.)
- * @version 1.1
- */
- class vkapi {
- var $api_secret;
- var $app_id;
- var $api_url;
- function vkapi($app_id, $api_secret, $oauth_token = '', $api_url = 'api.vk.com/api.php') {
- $this->app_id = $app_id;
- $this->token = $oauth_token;
- $this->api_secret = $api_secret;
- $this->oauth_url = "https://api.vk.com/";
- if (!strstr($api_url, 'http://')) $api_url = 'http://'.$api_url;
- $this->api_url = $api_url;
- }
- function api($method,$params=false,$oauth=false) {
- if($oauth==false){
- if (!$params) $params = array();
- $params['api_id'] = $this->app_id;
- $params['v'] = '3.0';
- $params['method'] = $method;
- $params['timestamp'] = time();
- $params['format'] = 'json';
- $params['random'] = rand(0,10000);
- ksort($params);
- $sig = '';
- foreach($params as $k=>$v) {
- $sig .= $k.'='.$v;
- }
- $sig .= $this->api_secret;
- $params['sig'] = md5($sig);
- $query = $this->api_url.'?'.$this->params($params);
- $res = file_get_contents($query);
- return json_decode($res, true);
- }else{
- $url="/method/".$method."?".$this->params($params)."&access_token=".$this->token;
- $res = $this->ssl($url);
- return json_decode($res, true);
- }
- }
- function token($code){
- $url = $this->ssl('/oauth/access_token?client_id='.$this->app_id.'&code='.$code.'&client_secret='.$this->api_secret);
- $data = json_decode($url, true);
- return $data;
- }
- function params($params) {
- $pice = array();
- foreach($params as $k=>$v) {
- $pice[] = $k.'='.urlencode($v);
- }
- return implode('&',$pice);
- }
- function ssl($req){
- $ch = curl_init($this->oauth_url.$req);
- curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_exec ($ch);
- return curl_multi_getcontent ($ch);
- curl_close ($ch);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment