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

VK Service key Add

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