├── .gitignore ├── LICENSE.md ├── README.md ├── composer.json ├── src ├── Api.php ├── Commands.php ├── Resource.php └── Sandbox.php └── tests └── ApiTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | .buildpath 2 | .idea 3 | .project 4 | .settings 5 | vendor 6 | composer.lock 7 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Piotr Jakubik 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # php-allegro-rest-api 2 | Simple interface for Allegro REST API resources 3 | 4 | ## Authorization and Tokens ## 5 | In order to use Allegro REST Api, you have to register your application and authorize it (https://developer.allegroapi.io/auth/). 6 | 7 | ### Authorization link ### 8 | ```php 9 | $api = new Api($clientId, $clientSecret, $apiKey, $redirectUri, null, null); 10 | echo $api->getAuthorizationUri(); 11 | ``` 12 | 13 | ### Getting new token ### 14 | ```php 15 | # example contents of your_redirect_uri.com/index.php 16 | $code = $_GET['code']; 17 | $api = new Api($clientId, $clientSecret, $apiKey, $redirectUri, null, null); 18 | $response = $api->getNewAccessToken($code); 19 | # response contains json with your access_token and refresh_token 20 | ``` 21 | 22 | ### Refreshing existing token ### 23 | ```php 24 | $api = new Api($clientId, $clientSecret, $apiKey, $redirectUri, $accessToken, $refreshToken); 25 | $response = $api->refreshAccessToken(); 26 | # response contains json with your new access_token and refresh_token 27 | ``` 28 | 29 | ## Example usage ## 30 | ```php 31 | $api = new Api($clientId, $clientSecret, $apiKey, $redirectUri, $accessToken, $refreshToken); 32 | 33 | // GET https://allegroapi.io/{resource} 34 | // $api->{resource}->get(); 35 | 36 | // GET https://allegroapi.io/categories 37 | $api->categories->get(); 38 | 39 | // GET https://allegroapi.io/{resource}/{resource_id} 40 | // $api->{resource}({resource_id})->get(); 41 | 42 | // GET https://allegroapi.io/categories/2 43 | $api->categories(2)->get(); 44 | 45 | // PUT https://allegroapi.io/{resource}/{resource_id}/{command-name}-command/{uuid} 46 | // $api->{resource}({resource_id})->commands()->{command_name}($data); 47 | 48 | // PUT https://allegroapi.io/offers/12345/change-price-commands/84c16171-233a-42de-8115-1f1235c8bc0f 49 | $api->offers(12345)->commands()->change_price($data); 50 | ``` 51 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wiatrogon/allegro-rest-api", 3 | "description": "simple interface for Allegro REST API", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "Piotr Jakubik", 8 | "email": "wiatrogon@gmail.com" 9 | } 10 | ], 11 | "keywords" : [ 12 | "allegro", 13 | "rest" 14 | ], 15 | "require": { 16 | "php": ">=5.3" 17 | }, 18 | "autoload": { 19 | "psr-4": { 20 | "Allegro\\REST\\": "src" 21 | } 22 | }, 23 | "require-dev": { 24 | "phpunit/phpunit": "^5.7" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Api.php: -------------------------------------------------------------------------------- 1 | clientId = $clientId; 26 | $this->clientSecret = $clientSecret; 27 | $this->apiKey = $apiKey; 28 | $this->redirectUri = $redirectUri; 29 | $this->accessToken = $accessToken; 30 | $this->refreshToken = $refreshToken; 31 | } 32 | 33 | /** 34 | * @return string 35 | */ 36 | public function getUri() 37 | { 38 | return static::API_URI; 39 | } 40 | 41 | /** 42 | * @return null|string 43 | */ 44 | public function getAccessToken() 45 | { 46 | return $this->accessToken; 47 | } 48 | 49 | /** 50 | * @return string 51 | */ 52 | public function getApiKey() 53 | { 54 | return $this->apiKey; 55 | } 56 | 57 | /** 58 | * @return string 59 | */ 60 | public function getAuthorizationUri() 61 | { 62 | $data = array( 63 | 'response_type' => 'code', 64 | 'client_id' => $this->clientId, 65 | 'api-key' => $this->apiKey, 66 | 'redirect_uri' => $this->redirectUri 67 | ); 68 | 69 | return static::AUTHORIZATION_URI . '?' . http_build_query($data); 70 | } 71 | 72 | /** 73 | * @param string $code 74 | * @return object 75 | */ 76 | public function getNewAccessToken($code) 77 | { 78 | $data = array( 79 | 'grant_type' => 'authorization_code', 80 | 'code' => $code, 81 | 'api-key' => $this->apiKey, 82 | 'redirect_uri' => $this->redirectUri 83 | ); 84 | 85 | return $this->requestAccessToken($data); 86 | } 87 | 88 | /** 89 | * @return object 90 | */ 91 | public function refreshAccessToken() 92 | { 93 | $data = array( 94 | 'grant_type' => 'refresh_token', 95 | 'api-key' => $this->apiKey, 96 | 'refresh_token' => $this->refreshToken, 97 | 'redirect_uri' => $this->redirectUri 98 | ); 99 | 100 | return $this->requestAccessToken($data); 101 | } 102 | 103 | /** 104 | * @param array $data 105 | * @return object 106 | */ 107 | private function requestAccessToken($data) 108 | { 109 | $authorization = base64_encode($this->clientId . ':' . $this->clientSecret); 110 | 111 | $headers = array( 112 | "Authorization: Basic $authorization", 113 | "Content-Type: application/x-www-form-urlencoded" 114 | ); 115 | 116 | $data = http_build_query($data); 117 | 118 | $response = $this->sendHttpRequest(static::TOKEN_URI, 'POST', $headers, $data); 119 | 120 | $data = json_decode($response); 121 | 122 | if (isset($data->access_token) && isset($data->refresh_token)) 123 | { 124 | $this->accessToken = $data->access_token; 125 | $this->refreshToken = $data->refresh_token; 126 | } 127 | 128 | return $response; 129 | } 130 | 131 | /** 132 | * @var string 133 | */ 134 | protected $clientId; 135 | 136 | /** 137 | * @var string 138 | */ 139 | protected $clientSecret; 140 | 141 | /** 142 | * @var string 143 | */ 144 | protected $apiKey; 145 | 146 | /** 147 | * @var string 148 | */ 149 | protected $redirectUri; 150 | 151 | /** 152 | * @var string 153 | */ 154 | protected $accessToken; 155 | 156 | /** 157 | * @var string 158 | */ 159 | protected $refreshToken; 160 | } 161 | -------------------------------------------------------------------------------- /src/Commands.php: -------------------------------------------------------------------------------- 1 | resource = $resource; 13 | } 14 | 15 | public function __call($name, $args) 16 | { 17 | $data = array_shift($args); 18 | $name = str_replace('_', '-', $name) . '-commands'; 19 | $type = new Resource($name, $this->resource); 20 | $command = new Resource($this->getUuid(), $type); 21 | 22 | return $command->put($data); 23 | } 24 | 25 | /** 26 | * @return string 27 | */ 28 | private function getUuid() 29 | { 30 | return sprintf( 31 | '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', 32 | mt_rand(0, 0xffff), 33 | mt_rand(0, 0xffff), 34 | mt_rand(0, 0xffff), 35 | mt_rand(0, 0x0fff) | 0x4000, 36 | mt_rand(0, 0x3fff) | 0x8000, 37 | mt_rand(0, 0xffff), 38 | mt_rand(0, 0xffff), 39 | mt_rand(0, 0xffff) 40 | ); 41 | } 42 | 43 | /** 44 | * @var Resource 45 | */ 46 | private $resource; 47 | } 48 | -------------------------------------------------------------------------------- /src/Resource.php: -------------------------------------------------------------------------------- 1 | id = $id; 15 | $this->parent = $parent; 16 | } 17 | 18 | /** 19 | * @return mixed 20 | */ 21 | public function getAccessToken() 22 | { 23 | return $this->parent->getAccessToken(); 24 | } 25 | 26 | /** 27 | * @return mixed 28 | */ 29 | public function getApiKey() 30 | { 31 | return $this->parent->getApiKey(); 32 | } 33 | 34 | /** 35 | * @return string 36 | */ 37 | public function getUri() 38 | { 39 | return $this->parent->getUri() . '/' . $this->id; 40 | } 41 | 42 | /** 43 | * @return Commands 44 | */ 45 | public function commands() 46 | { 47 | return new Commands($this); 48 | } 49 | 50 | /** 51 | * @param null|array $data 52 | * @return bool|string 53 | */ 54 | public function get($data = null) 55 | { 56 | $uri = $this->getUri(); 57 | 58 | if ($data !== null) { 59 | $uri .= '?'; 60 | $uri .= http_build_query($data); 61 | } 62 | 63 | return $this->sendApiRequest($uri, 'GET'); 64 | } 65 | 66 | /** 67 | * @param array $data 68 | * @return bool|string 69 | */ 70 | public function put($data) 71 | { 72 | return $this->sendApiRequest($this->getUri(), 'PUT', $data); 73 | } 74 | 75 | /** 76 | * @param array $data 77 | * @return bool|string 78 | */ 79 | public function post($data) 80 | { 81 | return $this->sendApiRequest($this->getUri(), 'POST', $data); 82 | } 83 | 84 | /** 85 | * @param null|array $data 86 | * @return bool|string 87 | */ 88 | public function delete($data = null) 89 | { 90 | $uri = $this->getUri(); 91 | 92 | if ($data !== null) { 93 | $uri .= '?'; 94 | $uri .= http_build_query($data); 95 | } 96 | 97 | return $this->sendApiRequest($uri, 'DELETE'); 98 | } 99 | 100 | public function __get($name) 101 | { 102 | return new Resource($name, $this); 103 | } 104 | 105 | public function __call($name, $args) 106 | { 107 | $id = array_shift($args); 108 | $collection = new Resource($name, $this); 109 | return new Resource($id, $collection); 110 | } 111 | 112 | /** 113 | * @param string $url 114 | * @param string $method 115 | * @param array $data 116 | * @return bool|string 117 | */ 118 | protected function sendApiRequest($url, $method, $data = array()) 119 | { 120 | $token = $this->getAccessToken(); 121 | $key = $this->getApiKey(); 122 | 123 | $headers = array( 124 | "Authorization: Bearer $token", 125 | "Api-Key: $key", 126 | "Content-Type: application/vnd.allegro.public.v1+json", 127 | "Accept: application/vnd.allegro.public.v1+json" 128 | ); 129 | 130 | $data = json_encode($data); 131 | 132 | return $this->sendHttpRequest($url, $method, $headers, $data); 133 | } 134 | 135 | /** 136 | * @param string $url 137 | * @param string $method 138 | * @param array $headers 139 | * @param string $data 140 | * @return bool|string 141 | */ 142 | protected function sendHttpRequest($url, $method, $headers = array(), $data = '') 143 | { 144 | $options = array( 145 | 'http' => array( 146 | 'method' => $method, 147 | 'header' => implode("\r\n", $headers), 148 | 'content' => $data, 149 | 'ignore_errors' => true 150 | ) 151 | ); 152 | 153 | $context = stream_context_create($options); 154 | 155 | return file_get_contents($url, false, $context); 156 | } 157 | 158 | /** 159 | * @var string 160 | */ 161 | private $id; 162 | 163 | /** 164 | * @var Resource 165 | */ 166 | private $parent; 167 | } 168 | -------------------------------------------------------------------------------- /src/Sandbox.php: -------------------------------------------------------------------------------- 1 | assertEquals('https://allegroapi.io', $api->getUri()); 16 | 17 | $this->assertEquals('https://allegroapi.io/categories', 18 | $api->categories->getUri()); 19 | 20 | $this->assertEquals('https://allegroapi.io/categories/12', 21 | $api->categories(12)->getUri()); 22 | } 23 | 24 | /** 25 | * @dataProvider credentialsProvider 26 | */ 27 | function testAuthorization($clientId, $clientSecret, $apiKey, 28 | $redirectUri, $accessToken, $refreshToken) 29 | { 30 | $api = new Allegro\REST\Api($clientId, $clientSecret, $apiKey, 31 | $redirectUri, $accessToken, $refreshToken); 32 | 33 | $expected = 'https://ssl.allegro.pl/auth/oauth/authorize' . 34 | "?response_type=code&client_id=$clientId" . 35 | "&api-key=$apiKey&redirect_uri=$redirectUri"; 36 | 37 | $this->assertEquals($expected, $api->getAuthorizationUri()); 38 | 39 | $this->assertEquals($accessToken, $api->getAccessToken()); 40 | $this->assertEquals($accessToken, $api->categories->getAccessToken()); 41 | $this->assertEquals($accessToken, $api->categories(123)->getAccessToken()); 42 | 43 | $this->assertEquals($apiKey, $api->getApiKey()); 44 | $this->assertEquals($apiKey, $api->categories->getApiKey()); 45 | $this->assertEquals($apiKey, $api->categories(123)->getApiKey()); 46 | } 47 | 48 | function credentialsProvider() 49 | { 50 | return array( 51 | array('eggs', 'spam', 'ham', 'beans', null, null), 52 | array('wood', 'stone', 'clay', 'wool', 'wheat', 'depleted uranium'), 53 | array('white', 'blue', 'black', 'red', 'green', 'colorless') 54 | ); 55 | } 56 | } 57 | --------------------------------------------------------------------------------