├── .gitignore
├── tests
├── bootstrap.php
├── ControllerTest.php
├── ResponseTest.php
├── SignatureProcessorTest.php
├── RequestTest.php
└── SurvariumApiTest.php
├── phpunit.xml
├── src
├── SurvariumException.php
├── Consumer.php
├── JsonTransformer.php
├── Controller.php
├── SignatureProcessor.php
├── Curl.php
├── Response.php
├── Request.php
└── SurvariumApi.php
├── composer.json
├── README.md
├── example.php
└── autoload.php
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea/
2 | .git/
3 |
4 |
--------------------------------------------------------------------------------
/tests/bootstrap.php:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 | tests/
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/src/SurvariumException.php:
--------------------------------------------------------------------------------
1 |
8 | * @version 1.0
9 | * @link survarium.com
10 | */
11 | namespace Survarium\Api;
12 |
13 | class SurvariumException extends \Exception
14 | {
15 |
16 | }
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name" : "survarium/api",
3 | "description" : "Survarium API For community",
4 | "type" : "library",
5 | "homepage": "https://survarium.com",
6 | "version" : "1.0",
7 | "require" : {
8 | "php" : ">=5.5",
9 | "ext-curl": "*"
10 | },
11 | "require-dev" : {
12 | "phpunit/phpunit" : "4.*"
13 | },
14 | "autoload": {
15 | "psr-4": {
16 | "Survarium\\Api\\": "src"
17 | }
18 | }
19 | }
--------------------------------------------------------------------------------
/tests/ControllerTest.php:
--------------------------------------------------------------------------------
1 |
6 | * @version 1.0
7 | */
8 |
9 | namespace Survarium\Api\Tests;
10 |
11 | use Survarium\Api\Controller;
12 |
13 |
14 | class ControllerTest extends \PHPUnit_Framework_TestCase
15 | {
16 | public function testReceivingRequest()
17 | {
18 | $controller = new Controller('test', 'test');
19 | $result = $controller->sendGetRequest('getmaxmatchid', ['pid' => '17083592333428139024']);
20 | $this->assertInstanceOf('\Survarium\Api\Response', $result);
21 | $this->assertEquals(200, $result->getStatusCode());
22 | }
23 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Survarium API
2 | It is a recommended example of API client for api.survarium.com.
3 | You can request some statistic information about Survarium with help of API.
4 |
5 | ## Usage
6 | You can install it through composer by adding next lines to your composer.json
7 |
8 | ```
9 | {
10 | "require" : {
11 | "survarium/api" : "dev-master@dev"
12 | }
13 | }
14 | ```
15 |
16 | If you do not use composer you should add next line to your php script
17 |
18 | ```
19 | require DIR . '/autoload.php';
20 | ```
21 | where DIR is filepath to root library directory.
22 | You can use $survariumApi object to retrieve necessary data:
23 |
24 | ```
25 | $survariumApi = new \Survarium\Api\SurvariumApi('test', 'test');
26 | $maxMatchId = $survariumApi->getMaxMatchId();
27 | ```
28 |
29 | API server is under development now. You can use it only for test now.
30 | In order to get personal credentials for our API, please, use http://api.survarium.com/register
31 |
32 |
33 |
--------------------------------------------------------------------------------
/example.php:
--------------------------------------------------------------------------------
1 | ';
7 | print_r($api->getMaxMatchId());
8 | print_r($api->matchesCountByPublicId('17083592333428139024'));
9 | print_r($api->getPublicIdByNickname('Убить врага'));
10 | print_r($api->getMatchesIdByPublicId('17083592333428139024', 20, 0));
11 | print_r($api->getNicknamesByPublicIds(['17083592333428139024', '7542784095223883934']));
12 | print_r($api->getMatchStatistic(3200430, 'russian'));
13 | print_r($api->getUserData('17083592333428139024', 'russian'));
14 | print_r($api->getClanAmounts());
15 | print_r($api->getClans(20, 0));
16 | print_r($api->getClanInfo(1));
17 | print_r($api->getClanMembers(1));
18 | print_r($api->getSlotsDict('english'));
19 | print_r($api->getItemsDict('russian'));
20 | print_r($api->getMapsDict('russian'));
21 | print_r($api->getNewMatches(1451001600, 10));
22 | print_r($api->getUserSkills('12886876036947614728'));
23 | echo '';
24 |
--------------------------------------------------------------------------------
/src/Consumer.php:
--------------------------------------------------------------------------------
1 |
10 | * @version 1.0
11 | * @link survarium.com
12 | */
13 |
14 | namespace Survarium\Api;
15 |
16 | class Consumer
17 | {
18 | private $sharedKey;
19 |
20 | private $privateKey;
21 |
22 | /**
23 | * @param $sharedKey
24 | * @param $privateKey
25 | */
26 | public function __construct($sharedKey, $privateKey)
27 | {
28 | $this->sharedKey = $sharedKey;
29 | $this->privateKey = $privateKey;
30 | }
31 |
32 | /**
33 | * @return mixed
34 | */
35 | public function getSharedKey()
36 | {
37 | return $this->sharedKey;
38 | }
39 |
40 | /**
41 | * @return mixed
42 | */
43 | public function getPrivateKey()
44 | {
45 | return $this->privateKey;
46 | }
47 | }
--------------------------------------------------------------------------------
/src/JsonTransformer.php:
--------------------------------------------------------------------------------
1 |
8 | * @version 1.0
9 | * @link survarium.com
10 | */
11 |
12 | namespace Survarium\Api;
13 |
14 | class JsonTransformer
15 | {
16 | /**
17 | * @param array $array
18 | * @return string
19 | */
20 | public static function encode(array $array)
21 | {
22 | return json_encode($array);
23 | }
24 |
25 | /**
26 | * @param $jsonString
27 | * @param bool $asArray
28 | * @return mixed
29 | * @throws SurvariumException
30 | */
31 | public static function decode($jsonString, $asArray = true)
32 | {
33 | if (!is_string($jsonString)) {
34 | throw new SurvariumException('JsonDecode param should be a string');
35 | }
36 | return json_decode($jsonString, $asArray, 256, JSON_BIGINT_AS_STRING);
37 | }
38 | }
--------------------------------------------------------------------------------
/autoload.php:
--------------------------------------------------------------------------------
1 |
6 | * @version 1.0
7 | */
8 |
9 | namespace Survarium\Api\Tests;
10 |
11 | use Survarium\Api\Response;
12 |
13 | /**
14 | * @coverDefaultClass Survarium\Api\Response
15 | */
16 | class ResponseTest extends \PHPUnit_Framework_TestCase
17 | {
18 |
19 | /**
20 | * @covers ::setHeadersString
21 | */
22 | public function testParseHeaders()
23 | {
24 | $headerString = <<setHeadersString($headerString);
38 | $responseHeaders = $response->getHeaders();
39 | $expectedArray = [
40 | 'Date' => 'Wed, 02 Dec 2015 11:33:54 GMT',
41 | 'Content-Type' => 'text/html; charset=utf-8',
42 | 'Transfer-Encoding' => 'chunked',
43 | 'Connection' => 'keep-alive',
44 | 'Cache-Control' => 'public, no-cache=\"Set-Cookie\", max-age=35',
45 | 'Expires' => 'Wed, 02 Dec 2015 11:34:29 GMT',
46 | 'Last-Modified' => 'Wed, 02 Dec 2015 11:33:29 GMT',
47 | 'Vary' => '*',
48 | 'Server' => 'nginx'
49 | ];
50 |
51 | $this->assertEquals($expectedArray, $responseHeaders);
52 | }
53 | }
--------------------------------------------------------------------------------
/src/Controller.php:
--------------------------------------------------------------------------------
1 |
9 | * @version 1.0
10 | * @link survarium.com
11 | */
12 |
13 | namespace Survarium\Api;
14 |
15 | class Controller
16 | {
17 | protected $request;
18 |
19 | protected $response;
20 |
21 | protected $signatureProcessor;
22 |
23 | /**
24 | * We can not require ssl from our community so we use http
25 | * @var string
26 | */
27 | const baseUrl = 'http://api.survarium.com/';
28 |
29 | protected $consumer;
30 |
31 | function __construct($sharedKey, $privateKey)
32 | {
33 | $this->signatureProcessor = new SignatureProcessor();
34 | $this->consumer = new Consumer($sharedKey, $privateKey);
35 | }
36 |
37 | /**
38 | * Wrapper for sending GET requests
39 | *
40 | * @param $path
41 | * @param $urlParams
42 | * @throws SurvariumException
43 | * @return Response
44 | */
45 | public function sendGetRequest($path, $urlParams)
46 | {
47 | return $this->sendHttpRequest('GET', $path, $urlParams);
48 | }
49 |
50 | /**
51 | * Send Http request and return response
52 | *
53 | * For now we need only get requests
54 | *
55 | * @param $method
56 | * @param $path
57 | * @param $params
58 | * @throws SurvariumException
59 | * @return Response
60 | */
61 | private function sendHttpRequest($method, $path, $params)
62 | {
63 | $this->request = new Request($method, $path, $params, $this->consumer, $this->signatureProcessor);
64 | $this->response = new Response();
65 | $transporter = new Curl($this->request, $this->response);
66 | $transporter->send();
67 | return $this->getResponse();
68 | }
69 |
70 | /**
71 | * Response getter
72 | *
73 | * @return mixed
74 | */
75 | public function getResponse()
76 | {
77 | return $this->response;
78 | }
79 | }
--------------------------------------------------------------------------------
/src/SignatureProcessor.php:
--------------------------------------------------------------------------------
1 |
8 | * @version 1.0
9 | * @link survarium.com
10 | */
11 |
12 | namespace Survarium\Api;
13 |
14 | class SignatureProcessor
15 | {
16 | /**
17 | * Chose acceptbale algo that exists on client side
18 | * @var array
19 | */
20 | protected $acceptedAlgos = [
21 | 'sha1'
22 | ];
23 |
24 | /**
25 | * Return algorithm name
26 | *
27 | * @return string
28 | */
29 | public function getName()
30 | {
31 | return 'HMAC-SHA1';
32 | }
33 |
34 | /**
35 | * Create signature
36 | *
37 | * @param Request $request
38 | * @param Consumer $consumer
39 | * @return string
40 | * @throws SurvariumException
41 | */
42 | public function buildSignature(Request $request, Consumer $consumer)
43 | {
44 | $algorithm = $this->getAcceptedAlgorithm();
45 | return base64_encode(hash_hmac($algorithm, $request->getSignatureString(), $consumer->getPrivateKey(), true));
46 | }
47 |
48 |
49 | /**
50 | * Check signature
51 | *
52 | * @param $signature
53 | * @param Request $request
54 | * @param Consumer $consumer
55 | * @return bool
56 | */
57 | public function checkSignature($signature, Request $request, Consumer $consumer)
58 | {
59 | $rightSignature = $this->buildSignature($request, $consumer);
60 | if ($rightSignature === $signature) {
61 | return true;
62 | }
63 | return false;
64 | }
65 |
66 | /**
67 | * Choose acceptable algorithm installed on client OS
68 | *
69 | * @return mixed
70 | * @throws SurvariumException
71 | */
72 | protected function getAcceptedAlgorithm()
73 | {
74 | $algos = hash_algos();
75 | foreach ($this->acceptedAlgos as $currentAlgo) {
76 | if (in_array($currentAlgo, $algos)) {
77 | return $currentAlgo;
78 | }
79 | }
80 | throw new SurvariumException('Install necessary hash algorithm (SHA1)');
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/tests/SignatureProcessorTest.php:
--------------------------------------------------------------------------------
1 |
6 | * @version 1.0
7 | */
8 |
9 | namespace Survarium\Api\Tests;
10 |
11 | use Survarium\Api\SignatureProcessor;
12 |
13 | /**
14 | * Class SignatureProcessorTest
15 | * @coverDefaultClass Survarium\Api\SignatureProcessor
16 | */
17 | class SignatureProcessorTest extends \PHPUnit_Framework_TestCase
18 | {
19 | /**
20 | * Adding ability to test protected and private methods
21 | */
22 | public static function getMethod($name)
23 | {
24 | $class = new \ReflectionClass('Survarium\Api\SignatureProcessor');
25 | $method = $class->getMethod($name);
26 | $method->setAccessible(true);
27 | return $method;
28 | }
29 |
30 | /**
31 | * Test if accepted method can be used
32 | * @covers ::getAcceptedAlgorithm
33 | */
34 | public function testAcceptedAlgorithm()
35 | {
36 | $sp = new SignatureProcessor();
37 | $getAcceptedAlgorithmMethod = self::getMethod('getAcceptedAlgorithm');
38 | $acceptedAlgo = $getAcceptedAlgorithmMethod->invoke($sp);
39 | $this->assertEquals('sha1', $acceptedAlgo);
40 | }
41 |
42 | /**
43 | * Check if client produce correct signature
44 | *
45 | * @dataProvider signatureDataProvider
46 | * @covers ::buildSignature
47 | */
48 | public function testBuildSignature($result, $requestString, $consumerString)
49 | {
50 | $request = $this->getMockBuilder('Survarium\Api\Request')->disableOriginalConstructor()->getMock();
51 | $request->method('getSignatureString')->willReturn($requestString);
52 |
53 | $consumer = $this->getMockBuilder('Survarium\Api\Consumer')->disableOriginalConstructor()->getMock();
54 | $consumer->method('getPrivateKey')->willReturn($consumerString);
55 |
56 | $sp = new SignatureProcessor();
57 | $signature = $sp->buildSignature($request, $consumer);
58 | $this->assertEquals($result, $signature);
59 | }
60 |
61 | public function signatureDataProvider()
62 | {
63 | return [
64 | ['f0/Wf8QkSkruaDWg48oMkWQGnDg=', 'test string' , 'test'],
65 | ['/IUIdFJpblvL47enH94A4yCvLMo=', '' , 'test'],
66 | ['wdBUMZAinjZHgKuvr7+0Ze6N1do=', 'test string' , ''],
67 | ];
68 | }
69 | }
--------------------------------------------------------------------------------
/src/Curl.php:
--------------------------------------------------------------------------------
1 |
8 | * @version 1.0
9 | * @link survarium.com
10 | */
11 | namespace Survarium\Api;
12 |
13 | class Curl
14 | {
15 | protected $curl;
16 |
17 | protected $request;
18 |
19 | protected $response;
20 |
21 | protected $defaultOptions = [
22 | CURLOPT_MAXREDIRS => 0,
23 | CURLOPT_FOLLOWLOCATION => 0,
24 | CURLOPT_RETURNTRANSFER => true,
25 | CURLOPT_ENCODING => 'gzip',
26 | CURLOPT_HEADER => true,
27 | CURLOPT_TIMEOUT => 3,
28 | CURLOPT_USERAGENT => 'Survarium browser'
29 | ];
30 |
31 | /**
32 | * @param Request $request
33 | * @param Response $response
34 | * @throws SurvariumException
35 | */
36 | public function __construct(Request $request, Response $response)
37 | {
38 | if (!extension_loaded('curl')) {
39 | throw new SurvariumException('You should install curl extension');
40 | }
41 | $this->curl = curl_init();
42 | $this->request = $request;
43 | $this->response = $response;
44 | $this->setOptions(
45 | $request->getRequestUrl(),
46 | $request->getHeaders(),
47 | $request->getHttpMethod()
48 | );
49 | }
50 |
51 | /**
52 | * Now we always use GET method
53 | *
54 | * @param $url
55 | * @param $headers
56 | * @param string $method
57 | */
58 | public function setOptions($url, $headers, $method = 'GET')
59 | {
60 | $curlOptions = [
61 | CURLOPT_URL => $url,
62 | CURLOPT_HTTPHEADER => $headers,
63 | ];
64 | $resultOptions = $this->defaultOptions + $curlOptions;
65 | curl_setopt_array($this->curl, $resultOptions);
66 | }
67 |
68 | /**
69 | * Send Curl request and set request data to request object
70 | *
71 | * @throws SurvariumException
72 | */
73 | public function send()
74 | {
75 | $response = curl_exec($this->curl);
76 | if (curl_errno($this->curl) > 0) {
77 | throw new SurvariumException(curl_error($this->curl), curl_errno($this->curl));
78 | }
79 | $this->response->setStatusCode(curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
80 | //split headers and body
81 | $parts = explode("\r\n\r\n", $response);
82 | $responseBody = array_pop($parts);
83 | $responseHeader = array_pop($parts);
84 | $this->response->setHeadersString($responseHeader);
85 | $this->response->setBodyString($responseBody);
86 | curl_close($this->curl);
87 | }
88 | }
89 |
90 |
91 |
--------------------------------------------------------------------------------
/src/Response.php:
--------------------------------------------------------------------------------
1 |
8 | * @version 1.0
9 | * @link survarium.com
10 | * @todo add ability to supply body in different formats
11 | */
12 |
13 | namespace Survarium\Api;
14 |
15 | class Response
16 | {
17 | /**
18 | * @var string
19 | */
20 | protected $body;
21 |
22 | /**
23 | * @var array of headers
24 | */
25 | protected $headers;
26 |
27 | protected $statusCode = 0;
28 |
29 | /**
30 | * @return mixed
31 | */
32 | public function getBody()
33 | {
34 | $result = null;
35 | if (!empty($this->headers['Content-Type'])) {
36 | switch ($this->headers['Content-Type'])
37 | {
38 | case 'application/json':
39 | $result = JsonTransformer::decode($this->body);
40 | break;
41 | default:
42 | $result = $this->body;
43 | }
44 | }
45 |
46 | return $result;
47 | }
48 |
49 | /**
50 | * @return mixed
51 | */
52 | public function getHeaders()
53 | {
54 | return $this->headers;
55 | }
56 |
57 | /**
58 | * @param $body
59 | */
60 | public function setBodyString($body)
61 | {
62 | $this->body = $this->parseBody($body);
63 | }
64 |
65 | /**
66 | * @param $headers
67 | */
68 | public function setHeadersString($headers)
69 | {
70 | $this->headers = $this->parseHeaders($headers);
71 | }
72 |
73 | /**
74 | * @return int
75 | */
76 | public function getStatusCode()
77 | {
78 | return $this->statusCode;
79 | }
80 |
81 | /**
82 | * @param $code
83 | */
84 | public function setStatusCode($code)
85 | {
86 | $this->statusCode = (int)$code;
87 | }
88 |
89 | /**
90 | * It takes raw header string return array of key value headers
91 | *
92 | * @param $headers
93 | * @return array
94 | */
95 | public function parseHeaders($headers)
96 | {
97 | $resultArray = [];
98 | $headerLines = explode("\r\n", $headers);
99 | foreach ($headerLines as $line) {
100 | if (strpos($line, ':') !== false) {
101 | list($key, $value) = explode(':', $line, 2);
102 | $resultArray[trim($key)] = trim($value);
103 | }
104 | }
105 | return $resultArray;
106 | }
107 |
108 | /**
109 | * Change body format
110 | *
111 | * Could be implemented later
112 | *
113 | * @param $body
114 | * @return mixed
115 | */
116 | protected function parseBody($body)
117 | {
118 | return $body;
119 | }
120 |
121 | }
--------------------------------------------------------------------------------
/tests/RequestTest.php:
--------------------------------------------------------------------------------
1 |
6 | * @version 1.0
7 | */
8 | namespace Survarium\Api\Tests;
9 |
10 | use Survarium\Api\Request;
11 |
12 | /**
13 | * @coverDefaultClass Survarium\Api\Request
14 | */
15 | class RequestTest extends \PHPUnit_Framework_TestCase
16 | {
17 | /**
18 | * Add ability to set private property
19 | * @param $property
20 | */
21 | protected function setPrivateProperty($object, $property, $value)
22 | {
23 | $rClass = new \ReflectionClass($object);
24 | $rProperty = $rClass->getProperty($property);
25 | $rProperty->setAccessible(true);
26 | $rProperty->setValue($object, $value);
27 | }
28 |
29 | protected function getController()
30 | {
31 | return $this->getMockBuilder('Survarium\Api\Controller')
32 | ->disableOriginalConstructor()
33 | ->getMock();
34 | }
35 |
36 | protected function getConsumer()
37 | {
38 | $consumer = $this->getMockBuilder('Survarium\Api\Consumer')
39 | ->setConstructorArgs(['test', 'test'])
40 | ->getMock();
41 | $consumer->method('getSharedKey')->willReturn('test');
42 | $consumer->method('getPrivateKey')->willReturn('test');
43 | return $consumer;
44 | }
45 |
46 | protected function getSignatureProcessor()
47 | {
48 | $sp = $this->getMockBuilder('Survarium\Api\SignatureProcessor')
49 | ->getMock();
50 | $sp->method('buildSignature')->willReturn('testSignature');
51 | $sp->method('getName')->willReturn('SHA1-HMAC');
52 | return $sp;
53 | }
54 |
55 | /**
56 | * Check if we create the right url
57 | *
58 | * @covers ::__construct
59 | * @covers ::signatureRequest
60 | * @covers ::createFullUrl
61 | */
62 | public function testConstructorWorksFine()
63 | {
64 | $request = new Request('GET', 'test', ['test' => 'test'], $this->getConsumer(), $this->getSignatureProcessor());
65 |
66 | //check full url was created well
67 | $this->assertEquals('http://api.survarium.com/test?test=test', $request->getRequestUrl());
68 | // test if signature request Works fine
69 | $this->assertEquals('test', $request->getAuthParam('surv_consumer_key'));
70 | $this->assertEquals('SHA1-HMAC', $request->getAuthParam('surv_signature_method'));
71 | $this->assertEquals('testSignature', $request->getAuthParam('surv_signature'));
72 | $this->assertNotEmpty($request->getAuthParam('surv_nonce'));
73 | $this->assertNotEmpty($request->getAuthParam('surv_timestamp'));
74 |
75 | return $request;
76 | }
77 |
78 | /**
79 | * @depends testConstructorWorksFine
80 | * @covers ::getHttpMethod
81 | */
82 | public function testGetHttpMethod(Request $request)
83 | {
84 | $method = $request->getHttpMethod();
85 | $this->assertEquals('GET', $method);
86 | }
87 |
88 | /**
89 | * @depends testConstructorWorksFine
90 | * @covers ::GetSignatureString
91 | */
92 | public function testGetSignatureString(Request $request)
93 | {
94 | $this->setPrivateProperty($request, 'timestamp', 1449069065);
95 | $this->setPrivateProperty($request, 'nonce', '79abde34395e05bb9abf2e2de0ba9be5');
96 | $string = $request->getSignatureString();
97 | $this->assertEquals('http://api.survarium.com/test?test=testGET79abde34395e05bb9abf2e2de0ba9be51449069065', $string);
98 | }
99 |
100 | /**
101 | * @depends testConstructorWorksFine
102 | * @covers ::parseHeaders
103 | * @covers ::getHeaders
104 | */
105 | public function testHeadersSetCorrectly(Request $request)
106 | {
107 | $headers = $request->getHeaders();
108 | $this->assertTrue(is_array($headers));
109 | $this->assertContains('Accept: application/json', $headers);
110 | }
111 | }
--------------------------------------------------------------------------------
/src/Request.php:
--------------------------------------------------------------------------------
1 |
8 | * @version 1.0
9 | * @link survarium.com
10 | * @todo Add more flexible ability to set Headers
11 | */
12 |
13 | namespace Survarium\Api;
14 |
15 | class Request
16 | {
17 | protected $headers = [];
18 |
19 | protected $body = null;
20 |
21 | /**
22 | * Url with GET params
23 | */
24 | protected $requestUrl;
25 |
26 | protected $nonce;
27 |
28 | protected $timestamp;
29 |
30 | protected $httpMethod;
31 |
32 | protected $authParams = [];
33 |
34 | public function __construct($method, $urlPath, array $urlParams = null, Consumer $consumer, SignatureProcessor $signatureProcessor)
35 | {
36 | $this->nonce = $this->generateNonce();
37 | $this->timestamp = time();
38 | $this->requestUrl = $this->createFullUrl($urlPath, $urlParams);
39 | $this->httpMethod = $method;
40 | $this->signatureRequest($consumer, $signatureProcessor);
41 | }
42 |
43 | /**
44 | * Creates absolute url
45 | *
46 | * @param $urlPath
47 | * @param array $urlParams
48 | * @return string
49 | */
50 | protected function createFullUrl($urlPath, array $urlParams)
51 | {
52 | $url = Controller::baseUrl . rawurldecode($urlPath);
53 | if (!empty($urlParams)) {
54 | $url .= '?' . http_build_query($urlParams);
55 | }
56 | return $url;
57 | }
58 |
59 | /**
60 | * Get absolute url
61 | *
62 | * @return string
63 | */
64 | public function getRequestUrl()
65 | {
66 | return $this->requestUrl;
67 | }
68 |
69 | /**
70 | * Create auth params for request
71 | *
72 | * @param Consumer
73 | */
74 | protected function signatureRequest(Consumer $consumer, SignatureProcessor $signatureProcessor)
75 | {
76 | $sign = $signatureProcessor->buildSignature($this, $consumer);
77 | $this->setAuthParams('surv_consumer_key', $consumer->getSharedKey());
78 | $this->setAuthParams('surv_nonce', $this->nonce);
79 | $this->setAuthParams('surv_signature', $sign);
80 | $this->setAuthParams('surv_signature_method', $signatureProcessor->getName());
81 | $this->setAuthParams('surv_timestamp', $this->timestamp);
82 | }
83 |
84 | /**
85 | * @return string
86 | */
87 | public function getSignatureString()
88 | {
89 | return $this->requestUrl . $this->httpMethod . $this->nonce . $this->timestamp;
90 | }
91 |
92 | /**
93 | * @param $key
94 | * @param $value
95 | */
96 | protected function setAuthParams($key, $value)
97 | {
98 | $this->authParams[$key] = $value;
99 | }
100 |
101 | /**
102 | * @param $key
103 | * @return bool
104 | */
105 | public function getAuthParam($key)
106 | {
107 | return isset($this->authParams[$key])? $this->authParams[$key] : false;
108 | }
109 |
110 | /**
111 | * Generate random string for signature
112 | *
113 | * @return string
114 | */
115 | protected function generateNonce()
116 | {
117 | return md5(mt_rand() . time());
118 | }
119 |
120 | /**
121 | * Combine necessary auth headers params to string
122 | *
123 | * @return string
124 | */
125 | protected function getAuthHeader()
126 | {
127 | $first = true;
128 | $output = 'OAuth ';
129 | foreach ($this->authParams as $key => $value) {
130 | $separator = ($first == true)? ' ' : ', ';
131 | $output .= $separator . rawurlencode($key) . '="' . rawurlencode($value) . '"' ;
132 | $first = false;
133 | }
134 | return $output;
135 | }
136 |
137 | /**
138 | * Get Headers array and return
139 | * @return array
140 | */
141 | public function getHeaders()
142 | {
143 | $authHeader = $this->getAuthHeader();
144 | return [
145 | 'Accept: application/json',
146 | 'Authorization:' . $authHeader,
147 | 'Expect:'
148 | ];
149 | }
150 |
151 | /**
152 | * @return mixed
153 | */
154 | public function getHttpMethod()
155 | {
156 | return $this->httpMethod;
157 | }
158 | }
--------------------------------------------------------------------------------
/tests/SurvariumApiTest.php:
--------------------------------------------------------------------------------
1 |
6 | * @version 1.0
7 | * @todo add more spesific assertion about received data
8 | */
9 |
10 | namespace Survarium\Api\Tests;
11 |
12 | use Survarium\Api\SurvariumApi;
13 |
14 | /**
15 | * @coverDefaultClass Survarium\Api\SurvariumApi
16 | */
17 | class SurvariumApiTest extends \PHPUnit_Framework_TestCase
18 | {
19 | private $api;
20 |
21 | public function setUp()
22 | {
23 | $this->api = new SurvariumApi('test', 'test');
24 | }
25 |
26 | /**
27 | * @covers ::getMaxMatchId
28 | */
29 | public function testGetMaxMatchId()
30 | {
31 | $result = $this->api->getMaxMatchId('17083592333428139024');
32 |
33 | $this->assertTrue(is_array($result));
34 | $this->assertEquals(200, $result['code']);
35 | }
36 |
37 | /**
38 | * @covers ::matchesCountByPublicId
39 | */
40 | public function testMatchesCountByPublicId()
41 | {
42 | $result = $this->api->matchesCountByPublicId('17083592333428139024');
43 |
44 | $this->assertTrue(is_array($result));
45 | $this->assertEquals(200, $result['code']);
46 | }
47 |
48 | /**
49 | * @covers ::getPublicIdByNickname
50 | */
51 | public function testGetPublicIdByNickname()
52 | {
53 | $result = $this->api->getPublicIdByNickname('Убить врага');
54 |
55 | $this->assertTrue(is_array($result));
56 | $this->assertEquals(200, $result['code']);
57 | }
58 |
59 | /**
60 | * @covers ::getMatchesIdByPublicId
61 | */
62 | public function testGetMatchesIdByPublicId()
63 | {
64 | $result = $this->api->getMatchesIdByPublicId('17083592333428139024', 20, 0);
65 |
66 | $this->assertTrue(is_array($result));
67 | $this->assertEquals(200, $result['code']);
68 | }
69 |
70 | /**
71 | * @covers ::getNicknamesByPublicIds
72 | */
73 | public function testGetNicknamesByPublicIds()
74 | {
75 | $result = $this->api->getNicknamesByPublicIds(['17083592333428139024', '7542784095223883934']);
76 |
77 | $this->assertTrue(is_array($result));
78 | $this->assertEquals(200, $result['code']);
79 | }
80 |
81 | /**
82 | * @covers ::getMatchStatistic
83 | */
84 | public function testGetMatchStatistic()
85 | {
86 | $result = $this->api->getMatchStatistic(3200430);
87 |
88 | $this->assertTrue(is_array($result));
89 | $this->assertEquals(200, $result['code']);
90 | }
91 |
92 | /**
93 | * @covers ::getUserData
94 | */
95 | public function testGetUserData()
96 | {
97 | $result = $this->api->getUserData('17083592333428139024');
98 |
99 | $this->assertTrue(is_array($result));
100 | $this->assertEquals(200, $result['code']);
101 | }
102 |
103 | /**
104 | * @covers ::getClanAmounts
105 | */
106 | public function testGetClanAmounts()
107 | {
108 | $result = $this->api->getClanAmounts();
109 | $this->assertTrue(is_array($result));
110 | $this->assertEquals(200, $result['code']);
111 | $this->assertTrue(is_array($result['data']));
112 | $this->assertEquals('get_clans_amount', $result['data']['action']);
113 | $this->assertTrue($result['data']['amount'] >0);
114 | }
115 |
116 | /**
117 | * @covers ::getClans
118 | */
119 | public function testGetClans()
120 | {
121 | $result = $this->api->getClans(20, 0);
122 | $this->assertTrue(is_array($result));
123 | $this->assertEquals(200, $result['code']);
124 | $this->assertTrue(is_array($result['data']));
125 | $this->assertEquals('get_clans', $result['data']['action']);
126 | $this->assertTrue(is_array($result['data']['clans_data']));
127 | $this->assertEquals(20, count($result['data']['clans_data']));
128 | }
129 |
130 | /**
131 | * @covers ::clanInfo
132 | */
133 | public function testClanInfo()
134 | {
135 | $result = $this->api->getClanInfo(1);
136 | $this->assertTrue(is_array($result));
137 | $this->assertEquals(200, $result['code']);
138 | $this->assertTrue(is_array($result['data']));
139 | $this->assertEquals('get_clan_info', $result['data']['action']);
140 | $this->assertTrue(is_array($result['data']['clan_info']));
141 | $this->assertEquals('1', $result['data']['clan_info']['id']);
142 | $this->assertEquals('Vostok Games', $result['data']['clan_info']['name']);
143 | $this->assertEquals('VG', $result['data']['clan_info']['abbreviation']);
144 | $this->assertTrue($result['data']['clan_info']['level'] > 0);
145 | $this->assertTrue($result['data']['clan_info']['elo'] > 0);
146 | }
147 |
148 | /**
149 | * @covers ::claninfo
150 | */
151 |
152 | public function testGetClanMembers()
153 | {
154 | $result = $this->api->getClanMembers(1);
155 | $this->assertTrue(is_array($result));
156 | $this->assertEquals(200, $result['code']);
157 | $this->assertTrue(is_array($result['data']));
158 | $this->assertEquals('get_clan_members_by_clanid', $result['data']['action']);
159 | $this->assertEquals('1', $result['data']['clan_id']);
160 | $this->assertTrue(is_array($result['data']['members']));
161 | $this->assertTrue(count($result['data']['members']) > 0);
162 | }
163 |
164 | /**
165 | * @covers ::getSlotsDict
166 | */
167 | public function testGetSlotsDict()
168 | {
169 | $result = $this->api->getSlotsDict('english');
170 | $this->assertTrue(is_array($result));
171 | $this->assertEquals(200, $result['code']);
172 | $this->assertTrue(is_array($result['data']));
173 | $this->assertEquals('get_slots_dict', $result['data']['action']);
174 | $this->assertEquals('helmet', $result['data']['dictionary'][0]);
175 | }
176 |
177 | /**
178 | * @covers ::getItemsDict
179 | */
180 | public function testGetItemsDict()
181 | {
182 | $result = $this->api->getItemsDict('russian');
183 | $this->assertTrue(is_array($result));
184 | $this->assertEquals(200, $result['code']);
185 | $this->assertTrue(is_array($result['data']));
186 | $this->assertEquals('get_items_dict', $result['data']['action']);
187 | $this->assertEquals('АК-74М', $result['data']['dictionary'][3]);
188 | }
189 |
190 | /**
191 | * @covers ::getMapsDict
192 | */
193 | public function testGetMapsDict()
194 | {
195 | $result = $this->api->getMapsDict('russian');
196 | $this->assertTrue(is_array($result));
197 | $this->assertEquals(200, $result['code']);
198 | $this->assertTrue(is_array($result['data']));
199 | $this->assertEquals('get_maps_dict', $result['data']['action']);
200 | $this->assertTrue($result['data']['dictionary'][0]['map_id'] > 0);
201 | }
202 |
203 | /**
204 | * @covers ::getMapsDict
205 | */
206 | public function testGetUserSkills()
207 | {
208 | $result = $this->api->getUserSkills('13269462395033796297');
209 | $this->assertTrue(is_array($result));
210 | $this->assertEquals(200, $result['code']);
211 | $this->assertTrue(is_array($result['data']));
212 | $this->assertTrue(count($result['data']['skills']) > 0);
213 | }
214 |
215 | }
--------------------------------------------------------------------------------
/src/SurvariumApi.php:
--------------------------------------------------------------------------------
1 |
11 | * @version 1.0
12 | * @link survarium.com
13 | */
14 |
15 | namespace Survarium\Api;
16 |
17 | class SurvariumApi
18 | {
19 | private $controller;
20 |
21 | public function __construct($sharedKey, $secretKey)
22 | {
23 | $this->controller = new Controller($sharedKey, $secretKey);
24 | }
25 |
26 | /**
27 | * Return max match_id played in Survarium
28 | * @param $pid
29 | * @return mixed
30 | */
31 | public function getMaxMatchId()
32 | {
33 | $params = [
34 | 'path' => 'getmaxmatchid',
35 | ];
36 | return $this->returnResponseBody($params);
37 | }
38 |
39 | /**
40 | * Return users public account id by $nickname
41 | *
42 | * @param $nickname
43 | * @return array
44 | */
45 | public function getPublicIdByNickname($nickname)
46 | {
47 | $params = [
48 | 'path' => 'getpublicidbynickname',
49 | 'params' => [
50 | 'nickname' => $nickname
51 | ]
52 | ];
53 | return $this->returnResponseBody($params);
54 | }
55 |
56 | /**
57 | * Return bunch of nicknames by array of public account ids
58 | *
59 | * @param array $pidArray
60 | * @return mixed
61 | */
62 | public function getNicknamesByPublicIds($pidArray)
63 | {
64 | $params = [
65 | 'path' => 'getnicknamesbypidarray',
66 | 'params' => [
67 | 'pids' => implode(',', $pidArray)
68 | ]
69 | ];
70 | return $this->returnResponseBody($params);
71 | }
72 |
73 | /**
74 | * Retrieve amount of played matches by user whose public account Id equals $pid
75 | *
76 | * @param $pid
77 | * @return mixed
78 | */
79 | public function matchesCountByPublicId($pid)
80 | {
81 | $params = [
82 | 'path' => 'getmatchescountbypid',
83 | 'params' => [
84 | 'pid' => $pid
85 | ]
86 | ];
87 | return $this->returnResponseBody($params);
88 | }
89 |
90 | /**
91 | * Return particular amount of played matches of user with public account id = $pid
92 | *
93 | * @param $pid
94 | * @param $matchAmount
95 | * @param $offset - offset from last played match ( by default 0 )
96 | * @return mixed
97 | */
98 | public function getMatchesIdByPublicId($pid, $matchAmount = 10, $offset = 0)
99 | {
100 | $params = [
101 | 'path' => 'getmatchesidbypublicid',
102 | 'params' => [
103 | 'pid' => $pid,
104 | 'matchAmount' => $matchAmount,
105 | 'offset' => $offset
106 | ]
107 | ];
108 | return $this->returnResponseBody($params);
109 | }
110 |
111 | /**
112 | * Return statistic of particular match by match_id
113 | *
114 | * @param $matchId
115 | * @param language
116 | * @return mixed
117 | */
118 | public function getMatchStatistic($matchId, $language = 'english')
119 | {
120 | $params = [
121 | 'path' => 'getmatchstatisticbyid',
122 | 'params' => [
123 | 'matchid' => $matchId,
124 | 'language' => $language
125 | ]
126 | ];
127 | return $this->returnResponseBody($params);
128 |
129 | }
130 |
131 | /**
132 | * Return all users data
133 | *
134 | * @param $pid
135 | * @param language
136 | * @return mixed
137 | */
138 | public function getUserData($pid, $language = 'english')
139 | {
140 | $params = [
141 | 'path' => 'getuserdatabypid',
142 | 'params' => [
143 | 'pid' => $pid,
144 | 'language' => $language
145 | ]
146 | ];
147 | return $this->returnResponseBody($params);
148 | }
149 |
150 | /**
151 | * Return amount of active clans in Survarium
152 | *
153 | * @return array
154 | */
155 | public function getClanAmounts()
156 | {
157 | $params = [
158 | 'path' => 'getclansamount',
159 | ];
160 | return $this->returnResponseBody($params);
161 | }
162 |
163 | /**
164 | * Return list of the clan ids and names ordered by elo reiting (begin from the top)
165 | *
166 | * @param $amount
167 | * @param $offset
168 | * @return array
169 | */
170 | public function getClans($amount, $offset)
171 | {
172 | $params = [
173 | 'path' => 'getclans',
174 | 'params' => [
175 | 'amount' => $amount,
176 | 'offset' => $offset
177 | ]
178 | ];
179 | return $this->returnResponseBody($params);
180 | }
181 |
182 | /**
183 | * Return name, abbr, level. elo reiting and pid of clan commander
184 | *
185 | * @param $clanId
186 | * @return array
187 | */
188 | public function getClanInfo($clanId)
189 | {
190 | $params = [
191 | 'path' => 'getclaninfo',
192 | 'params' => [
193 | 'clanid' => $clanId,
194 | ]
195 | ];
196 | return $this->returnResponseBody($params);
197 | }
198 |
199 | /**
200 | * Return members of given clan with their roles
201 | *
202 | * @param $clanId
203 | * @return array
204 | */
205 | public function getClanMembers($clanId)
206 | {
207 | $params = [
208 | 'path' => 'getclanmembers',
209 | 'params' => [
210 | 'clanid' => $clanId,
211 | ]
212 | ];
213 | return $this->returnResponseBody($params);
214 | }
215 |
216 | /**
217 | * @param $language
218 | * @return array
219 | */
220 | public function getSlotsDict($language)
221 | {
222 | $params = [
223 | 'path' => 'getslotsdict',
224 | 'params' => [
225 | 'language' => $language
226 | ]
227 | ];
228 | return $this->returnResponseBody($params);
229 | }
230 |
231 | /**
232 | * @param $language
233 | * @return array
234 | */
235 | public function getItemsDict($language)
236 | {
237 | $params = [
238 | 'path' => 'getitemsdict',
239 | 'params' => [
240 | 'language' => $language
241 | ]
242 |
243 | ];
244 | return $this->returnResponseBody($params);
245 | }
246 |
247 | public function getMapsDict($language)
248 | {
249 | $params = [
250 | 'path' => 'getmapsdict',
251 | 'params' => [
252 | 'language' => $language
253 | ]
254 |
255 | ];
256 | return $this->returnResponseBody($params);
257 | }
258 |
259 | public function getNewMatches($timestamp, $limit = 20, $offset = 0)
260 | {
261 | $params = [
262 | 'path' => 'getnewmatchesfrom',
263 | 'params' => [
264 | 'timestamp' => $timestamp,
265 | 'limit' => $limit,
266 | 'offset' => $offset
267 | ]
268 |
269 | ];
270 | return $this->returnResponseBody($params);
271 | }
272 |
273 | public function getUserSkills($pid)
274 | {
275 | $params = [
276 | 'path' => 'getuserskills',
277 | 'params' => [
278 | 'pid' => $pid
279 | ]
280 | ];
281 | return $this->returnResponseBody($params);
282 | }
283 |
284 | /**
285 | * Check if request was successful and return body of the request
286 | *
287 | * Here all SurvariumException could be Catched
288 | *
289 | * @param array $params - api path and params
290 | * @return array
291 | */
292 | private function returnResponseBody(array $params)
293 | {
294 | try {
295 | $urlParams = !empty($params['params'])? $params['params'] : [];
296 | $response = $this->controller->sendGetRequest($params['path'], $urlParams);
297 | return $this->returnResult($response->getStatusCode(), $response->getBody());
298 | } catch (SurvariumException $e) {
299 | return $this->returnResult($e->getCode(), $e->getMessage());
300 | }
301 | }
302 |
303 | /**
304 | * Return result array
305 | *
306 | * @param $result
307 | * @param $data
308 | * @return array
309 | */
310 | private function returnResult($result, $data)
311 | {
312 | return [
313 | 'code' => $result,
314 | 'data' => $data
315 | ];
316 | }
317 |
318 | }
319 |
320 |
--------------------------------------------------------------------------------