-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
a.pushkarev
committed
May 29, 2022
1 parent
1004b75
commit 761505d
Showing
21 changed files
with
436 additions
and
598 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,19 @@ | ||
.idea/ | ||
vendor/ | ||
examples/config.php | ||
# Logs | ||
logs | ||
*.log | ||
|
||
.env | ||
composer.phar | ||
composer.lock | ||
composer.lock | ||
vendor | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
!.vscode/extensions.json | ||
.idea | ||
.DS_Store | ||
*.suo | ||
*.ntvs* | ||
*.njsproj | ||
*.sln | ||
*.sw? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
use AXP\YaMetrika\Client; | ||
use AXP\YaMetrika\YaMetrika; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Middleware; | ||
use GuzzleHttp\MessageFormatter; | ||
use Monolog\Logger; | ||
|
||
$stack = HandlerStack::create(); | ||
$stack->push( | ||
Middleware::log( | ||
new Logger('Logger'), | ||
new MessageFormatter('{req_body} - {res_body}') | ||
) | ||
); | ||
|
||
$token = getenv('TOKEN'); | ||
$counterId = getenv('COUNTER_ID'); | ||
|
||
$client = new Client($token, $counterId, [ | ||
'handler' => $stack | ||
]); | ||
$metrika = new YaMetrika($client); | ||
|
||
$visitors = $metrika->getVisitors(); | ||
|
||
print_r($visitors->formatData()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
use AXP\YaMetrika\Client; | ||
use AXP\YaMetrika\YaMetrika; | ||
|
||
$token = getenv('TOKEN'); | ||
$counterId = getenv('COUNTER_ID'); | ||
$proxy = getenv('PROXY'); | ||
|
||
$client = new Client($token, $counterId, [ | ||
'proxy' => $proxy | ||
]); | ||
$metrika = new YaMetrika($client); | ||
|
||
$visitors = $metrika->getVisitors(); | ||
|
||
print_r($visitors->formatData()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
use AXP\YaMetrika\Client; | ||
use AXP\YaMetrika\YaMetrika; | ||
|
||
$token = getenv('TOKEN'); | ||
$counterId = getenv('COUNTER_ID'); | ||
|
||
$client = new Client($token, $counterId); | ||
$metrika = new YaMetrika($client); | ||
|
||
$visitors = $metrika->getVisitors(); | ||
|
||
print_r($visitors->formatData()); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace AXP\YaMetrika; | ||
|
||
use AXP\YaMetrika\Exception\ClientException; | ||
use GuzzleHttp\Client as HttpClient; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use GuzzleHttp\RequestOptions; | ||
|
||
/** | ||
* Class Client | ||
* | ||
* @author Alexander Pushkarev <axp-dev@yandex.com> | ||
* @link https://github.com/axp-dev/ya-metrika | ||
* @package AXP\YaMetrika | ||
*/ | ||
class Client | ||
{ | ||
private string $apiEndpoint = 'https://api-metrika.yandex.ru/stat/v1/data'; | ||
|
||
private HttpClient $httpClient; | ||
|
||
public function __construct( | ||
private string $token, | ||
private string $counterId, | ||
private array $httpClientOptions = [] | ||
) | ||
{ | ||
$this->httpClient = new HttpClient( | ||
$this->buildConfig($this->httpClientOptions) | ||
); | ||
} | ||
|
||
private function buildConfig(array $options): array | ||
{ | ||
return array_merge($options, [ | ||
RequestOptions::HEADERS => [ | ||
'Authorization' => "OAuth {$this->token}" | ||
] | ||
]); | ||
} | ||
|
||
private function buildUri(array $params = []): string | ||
{ | ||
$params['ids'] = $this->counterId; | ||
|
||
return $this->apiEndpoint . '?' . http_build_query(data: $params, arg_separator: '&'); | ||
} | ||
|
||
public function request($params): array | ||
{ | ||
try { | ||
$response = $this->httpClient->request('GET', $this->buildUri($params)); | ||
|
||
return json_decode($response->getBody(), true); | ||
} catch (GuzzleException $exception) { | ||
throw new ClientException($exception->getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace AXP\YaMetrika\Exception; | ||
|
||
/** | ||
* Class ClientException | ||
* | ||
* @author Alexander Pushkarev <axp-dev@yandex.com> | ||
* @link https://github.com/axp-dev/ya-metrika | ||
* @package AXP\YaMetrika\Exception | ||
*/ | ||
class ClientException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace AXP\YaMetrika\Exception; | ||
|
||
/** | ||
* Class ClientException | ||
* | ||
* @author Alexander Pushkarev <axp-dev@yandex.com> | ||
* @link https://github.com/axp-dev/ya-metrika | ||
* @package AXP\YaMetrika\Exception | ||
*/ | ||
class FormatException extends \Exception | ||
{ | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.