Commit 7b7a2df5 authored by Alex Ne's avatar Alex Ne

VK Service key Add

parent 12a985bf
<?php
namespace X\Social\VK;
class CredentialsClientIdError extends \X\ETrace\System {}
class CredentialsClientSecretError extends \X\ETrace\System {}
class Credentials {
/**
* @var mixed
*/
private $client_id, $client_secret, $api_version;
/**
* @param $client_id
* @param $client_secret
*/
public function __construct($client_id, $client_secret, $api_version = "5.52") {
if (is_numeric($client_id) && $client_id > 0) {
$this->client_id = $client_id;
} else {
throw new CredentialsClientIdError("VK Client ID not numeric or not > 0", 0, ["client_id" => $client_id]);
}
if (is_string($client_secret) && strlen($client_secret) > 0) {
$this->client_secret = $client_secret;
} else {
throw new CredentialsClientSecretError("VK Client Secret not string or len =< 0", 0, ["client_secret" => $client_secret]);
}
$this->api_version = $api_version;
}
/**
* @return int
*/
public function get_client_id() {
return $this->client_id;
}
/**
* @return string
*/
public function get_client_secret() {
return $this->client_secret;
}
/**
* @return string
*/
public function get_api_version() {
return $this->api_version;
}
}
<?php
namespace X\Social\VK;
class CredentialsClientIdError extends \X\ETrace\System {}
class CredentialsClientSecretError extends \X\ETrace\System {}
class Credentials {
/**
* @var mixed
*/
private $client_id, $client_secret, $api_version,$api_service_key;
/**
* @param $client_id
* @param $client_secret
*/
public function __construct($client_id, $client_secret, $api_version = "5.52", $api_service_key = false) {
if (is_numeric($client_id) && $client_id > 0) {
$this->client_id = $client_id;
} else {
throw new CredentialsClientIdError("VK Client ID not numeric or not > 0", 0, ["client_id" => $client_id]);
}
if (is_string($client_secret) && strlen($client_secret) > 0) {
$this->client_secret = $client_secret;
} else {
throw new CredentialsClientSecretError("VK Client Secret not string or len =< 0", 0, ["client_secret" => $client_secret]);
}
$this->api_version = $api_version;
$this->api_service_key = $api_service_key;
}
/**
* @return int
*/
public function get_client_id() {
return $this->client_id;
}
/**
* @return string
*/
public function get_client_secret() {
return $this->client_secret;
}
/**
* @return string
*/
public function get_api_version() {
return $this->api_version;
}
public function get_service_key() {
return $this->$api_service_key;
}
}
?>
\ No newline at end of file
<?php
namespace X\Social\VK;
class getServerTokenError extends \X\ETrace\System {}
class ServerToken extends VKAPI {
/**
* @var string
*/
private $oauth_url = "https://oauth.vk.com/";
/**
* @var mixed
*/
private $server_token;
/**
* @param \X\Social\VK\Credentials $Credentials
*/
public function __construct($Credentials = null) {
parent::__construct($Credentials);
$this->request();
}
private function request() {
$params["client_id"] = $this->Credentials->get_client_id();
$params["client_secret"] = $this->Credentials->get_client_secret();
$params["grant_type"] = "client_credentials";
$params["v"] = $this->Credentials->get_api_version();
$data = false;
$co = 0;
while ( ! $data && $co < 10) {
$data = $this->query($this->oauth_url . "access_token", $params);
if ( ! $data) {sleep(1);}
$co++;
}
if (isset($data["access_token"])) {
$this->server_token = $data["access_token"];
} else {
throw new getServerTokenError("VK: get server access_token fail", 0, ["params" => $params, "data" => $data]);
}
}
/**
* @return mixed
*/
public function get_token() {
return $this->server_token;
}
/**
* @return mixed
*/
public function getCredentials() {
return $this->Credentials;
}
}
<?php
namespace X\Social\VK;
class getServerTokenError extends \X\ETrace\System {}
class ServerToken extends VKAPI {
/**
* @var string
*/
private $oauth_url = "https://oauth.vk.com/";
/**
* @var mixed
*/
private $server_token;
/**
* @param \X\Social\VK\Credentials $Credentials
*/
public function __construct($Credentials = null) {
parent::__construct($Credentials);
$this->request();
}
private function request() {
$params["client_id"] = $this->Credentials->get_client_id();
$params["client_secret"] = $this->Credentials->get_client_secret();
$params["grant_type"] = "client_credentials";
$params["v"] = $this->Credentials->get_api_version();
$data = false;
$co = 0;
while ( ! $data && $co < 10) {
$data = $this->query($this->oauth_url . "access_token", $params);
if ( ! $data) {sleep(1);}
$co++;
}
if (isset($data["access_token"])) {
$this->server_token = $data["access_token"];
} else {
throw new getServerTokenError("VK: get server access_token fail", 0, ["params" => $params, "data" => $data]);
}
}
/**
* @return mixed
*/
public function get_token() {
return $this->server_token;
}
/**
* @return mixed
*/
public function getCredentials() {
return $this->Credentials;
}
}
?>
\ No newline at end of file
......@@ -2,6 +2,8 @@
namespace X\Social\VK;
use X\ETrace\System as ETSystem;
use X\Network\Http\Client as HttpClient;
use X\Social\VK\ServerToken as VKServerToken;
use VkConfig;
class VKAPICredentialsIsNull extends ETSystem {}
class VKAPIResponseError extends ETSystem {}
......@@ -35,6 +37,8 @@ class VKAPI {
if (is_null($this->Credentials)) {
throw new VKAPICredentialsIsNull("VK Credentials is NULL", 0);
}
}
/**
......@@ -46,7 +50,11 @@ class VKAPI {
if ( ! isset($params['v'])) {
$params["v"] = "5.28";
}
if ( ! isset($params['access_token'])) {
//$server_token = new VKServerToken($this->Credentials);
//$params["access_token"] = $server_token->get_token();
$params["access_token"] = $this->Credentials->get_service_key();
}
return $this->query($this->api_url . "method/" . $method, $params);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment