188 lines
5.7 KiB
PHP
188 lines
5.7 KiB
PHP
|
|
<?
|
||
|
|
class Vk{
|
||
|
|
|
||
|
|
const CALLBACK_BLANK = 'https://oauth.vk.com/blank.html';
|
||
|
|
const AUTHORIZE_URL = 'https://oauth.vk.com/authorize?client_id=5770058&scope=offline,wall,photos,groups&redirect_uri=https://oauth.vk.com/blank.html&display=page&v=5.60&response_type=token';
|
||
|
|
const GET_TOKEN_URL = 'https://oauth.vk.com/access_token?client_id=5770058&client_secret=UCwA3cDqr0A4878n4YGL&code={code}&redirect_uri=https://oauth.vk.com/blank.html';
|
||
|
|
const METHOD_URL = 'https://api.vk.com/method/';
|
||
|
|
|
||
|
|
public $secret_key = 'UCwA3cDqr0A4878n4YGL';
|
||
|
|
public $scope = array('offline','wall','photos','groups');
|
||
|
|
public $client_id = 5770058;
|
||
|
|
public $access_token = null;
|
||
|
|
public $owner_id = 0;
|
||
|
|
|
||
|
|
function __construct($options = array()){
|
||
|
|
|
||
|
|
$this->scope[]='offline';
|
||
|
|
|
||
|
|
if(count($options) > 0){
|
||
|
|
foreach($options as $key => $value){
|
||
|
|
if($key == 'scope' && is_string($value)){
|
||
|
|
$_scope = explode(',', $value);
|
||
|
|
$this->scope = array_merge($this->scope, $_scope);
|
||
|
|
} else {
|
||
|
|
$this->$key = $value;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Выполнение вызова Api метода
|
||
|
|
* @param string $method - метод, http://vk.com/dev/methods
|
||
|
|
* @param array $vars - параметры метода
|
||
|
|
* @return array - выводит массив данных или ошибку (но тоже в массиве)
|
||
|
|
*/
|
||
|
|
function api($method = '', $vars = array()){
|
||
|
|
|
||
|
|
$params = http_build_query($vars);
|
||
|
|
|
||
|
|
$url = $this->http_build_query($method, $params);
|
||
|
|
|
||
|
|
return (array)$this->call($url);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Построение конечного URI для выхова
|
||
|
|
* @param $method
|
||
|
|
* @param string $params
|
||
|
|
* @return string
|
||
|
|
*/
|
||
|
|
private function http_build_query($method, $params = ''){
|
||
|
|
return self::METHOD_URL . $method . '?' . $params.'&access_token=' . $this->access_token . '&v=5.82';
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Получить ссылка на запрос прав доступа
|
||
|
|
*
|
||
|
|
* @param string $type тип ответа (code - одноразовый код авторизации , token - готовый access token)
|
||
|
|
* @return mixed
|
||
|
|
*/
|
||
|
|
public function get_code_token($type="code"){
|
||
|
|
|
||
|
|
$url = self::AUTHORIZE_URL;
|
||
|
|
|
||
|
|
$scope = implode(',', $this->scope);
|
||
|
|
|
||
|
|
$url = str_replace('{client_id}', $this->client_id, $url);
|
||
|
|
$url = str_replace('{scope}', $scope, $url);
|
||
|
|
$url = str_replace('{redirect_uri}', self::CALLBACK_BLANK, $url);
|
||
|
|
$url = str_replace('{display}', 'page', $url);
|
||
|
|
$url = str_replace('{response_type}', $type, $url);
|
||
|
|
|
||
|
|
return $url;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public function get_token($code){
|
||
|
|
|
||
|
|
$url = self::GET_TOKEN_URL;
|
||
|
|
$url = str_replace('{code}', $code, $url);
|
||
|
|
|
||
|
|
return $this->call($url);
|
||
|
|
}
|
||
|
|
|
||
|
|
function call($url = ''){
|
||
|
|
if(function_exists('curl_init')) $json = $this->curl_post($url); else $json = file_get_contents($url);
|
||
|
|
|
||
|
|
$json = json_decode($json, true);
|
||
|
|
|
||
|
|
if(isset($json['response'])) return $json['response'];
|
||
|
|
|
||
|
|
return $json;
|
||
|
|
}
|
||
|
|
|
||
|
|
private function curl_post($url){
|
||
|
|
|
||
|
|
if(!function_exists('curl_init')) return false;
|
||
|
|
|
||
|
|
$param = parse_url($url);
|
||
|
|
|
||
|
|
if( $curl = curl_init() ) {
|
||
|
|
|
||
|
|
curl_setopt($curl, CURLOPT_URL, $param['scheme'].'://'.$param['host'].$param['path']);
|
||
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
|
||
|
|
curl_setopt($curl, CURLOPT_POST, true);
|
||
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $param['query']);
|
||
|
|
$out = curl_exec($curl);
|
||
|
|
|
||
|
|
curl_close($curl);
|
||
|
|
|
||
|
|
return $out;
|
||
|
|
}
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
/**
|
||
|
|
* @param array $options
|
||
|
|
*/
|
||
|
|
public function set_options($options = array()){
|
||
|
|
|
||
|
|
if(count($options) > 0){
|
||
|
|
foreach($options as $key => $value){
|
||
|
|
if($key == 'scope' && is_string($value)){
|
||
|
|
$_scope = explode(',', $value);
|
||
|
|
$this->scope = array_merge($this->scope, $_scope);
|
||
|
|
} else {
|
||
|
|
$this->$key = $value;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array $files
|
||
|
|
* @return array|bool
|
||
|
|
* @throws Exception
|
||
|
|
*/
|
||
|
|
function upload_photo($groupId, $userId, $files = array()){
|
||
|
|
|
||
|
|
if(count($files) == 0) return false;
|
||
|
|
if(!function_exists('curl_init')) return false;
|
||
|
|
|
||
|
|
$data_json = $this->api('photos.getWallUploadServer', array('group_id'=> $groupId));
|
||
|
|
|
||
|
|
if(!isset($data_json['upload_url'])) return false;
|
||
|
|
|
||
|
|
$temp = array_chunk($files, 5);
|
||
|
|
|
||
|
|
$attachments = array();
|
||
|
|
|
||
|
|
$upload_url = $data_json['upload_url'];
|
||
|
|
|
||
|
|
foreach ($temp[0] as $key => $data) {
|
||
|
|
|
||
|
|
$file = new CURLFile($data);
|
||
|
|
$ch = curl_init($upload_url);
|
||
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
||
|
|
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 1);
|
||
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
|
||
|
|
'photo' => $file
|
||
|
|
));
|
||
|
|
|
||
|
|
if (($upload = curl_exec($ch)) === false) {
|
||
|
|
throw new Exception(curl_error($ch));
|
||
|
|
}
|
||
|
|
|
||
|
|
curl_close($ch);
|
||
|
|
$upload_data = json_decode($upload, true);
|
||
|
|
$upload_data['group_id'] = $groupId;
|
||
|
|
$response = $this->api('photos.saveWallPhoto', $upload_data);
|
||
|
|
if (isset($response[0]) && isset($response[0]['id'])) {
|
||
|
|
$attachments[] = 'photo' . $userId . '_' .$response[0]['id'];
|
||
|
|
} else {
|
||
|
|
var_dump($response);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return $attachments;
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
?>
|