Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Предполагается, что сессия уже запущена, если нет - уберите комментарий
- // session_start();
- /**
- * @class VkApi
- * @author Maslakov Alexander <jmas.ukraine@gmail.com>
- */
- class VkApi
- {
- public $apiKey;
- public $appId;
- public $login;
- public $password;
- public $authRedirectUrl;
- public $apiUrl = 'https://api.vk.com/method/';
- public $v = '2.0';
- private $_sid;
- public function __construct($options)
- {
- foreach ($options as $key=>$value) {
- $this->{$key} = $value;
- }
- $this->_auth();
- }
- private function _auth()
- {
- $token = file_get_contents('./Cookies.txt');
- if (isset($_GET['code'])) {
- $url = 'https://oauth.vk.com/access_token?client_id='.$this->appId.'&client_secret='. $this->apiKey .'&code=' . $_GET['code'] . '&redirect_uri=' . $this->authRedirectUrl;
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_REFERER, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $responce = curl_exec($ch);
- curl_close($ch);
- $responce = json_decode($responce);
- if (isset($responce->access_token)) {
- file_put_contents('./Cookies.txt', $responce->access_token);
- $token = $responce->access_token;
- } else {
- throw new Exception('VK API error.');
- //var_dump($responce);exit;
- }
- }
- if (empty($token)) {
- $url = "https://oauth.vk.com/authorize?client_id="
- . $this->appId . "&redirect_uri=http://jmas.koding.com/agregator/vk.php&display=page&response_type=code&scope=video,offline";
- header('Location: ' . $url);
- exit;
- }
- $this->_accessToken = $token;
- }
- public function get($method, $params=false)
- {
- if (! $params) $params = array();
- $params['format'] = 'json';
- $url = $this->apiUrl . $method;
- $params['access_token'] = $this->_accessToken;
- ksort($params);
- $sig = '';
- foreach ($params as $k=>$v) {
- $sig .= $k.'='.$v;
- }
- $sig .= $this->apiKey;
- $params['sig'] = md5($sig);
- $query = $url . '?' . $this->_params($params);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_URL, $query);
- curl_setopt($ch, CURLOPT_REFERER, $query);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)");
- $res = curl_exec($ch);
- curl_close($ch);
- return json_decode($res, true);
- }
- public function getLikesCountByUrl($pageUrl)
- {
- if (parse_url($pageUrl, PHP_URL_HOST) != parse_url($this->authRedirectUrl, PHP_URL_HOST)) {
- throw new Exception('Page URL not valid!');
- }
- $request = $this->get('likes.getList', array(
- 'type' => 'sitepage',
- 'owner_id' => $this->appId,
- 'page_url' => $pageUrl,
- ));
- $this->_responce($request);
- }
- public function searchVideo($q, $offset)
- {
- $request = $this->get('video.search', array(
- 'q' => $q,
- 'offset' => $offset,
- ));
- return $this->_responce($request);
- }
- public function getWall($owner_id, $offset = 0, $count = 100, $filter = 'owner')
- {
- $request = $this->get('wall.get', array(
- 'owner_id' => '-' . $owner_id,
- 'offset' => $offset,
- 'count' => $count,
- 'filter' => $filter,
- ));
- return $this->_responce($request);
- }
- private function _params($params) {
- $pice = array();
- foreach($params as $k=>$v) {
- $pice[] = $k.'='.urlencode($v);
- }
- return implode('&',$pice);
- }
- private function _responce($request)
- {
- if (isset($request['response'])) {
- return $request['response'];
- } else if (isset($request['error'])) {
- throw new Exception($request['error']['error_msg']);
- }
- return null;
- }
- }
- // Пример использования
- $vk = new VkApi(array(
- 'apiKey' => '<API_KEY>',
- 'appId' => '<APP_ID>',
- 'login' => '<LOGIN>',
- 'password' => '<PASSWORD>',
- 'authRedirectUrl' => '<URL_OF_THIS_PAGE>',
- ));
- echo '<pre>';
- var_dump($vk->getWall($_GET['owner_id']));
- echo '</pre>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement