├── .gitignore ├── README.md ├── composer.json ├── config └── config.example.ini ├── src └── AllegroApi │ ├── AllegroApi.php │ ├── AllegroApiException.php │ └── AllegroConfig.php └── tests ├── AllegroApiTest.php └── ConfigDistributor.php /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | /config/config.ini 3 | /vendor/ 4 | /.idea/ 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP-AllegroApi 2 | A newest library to communicate with Allegro Api. The library is OOP, good tested and easy to use. Make your job enjoyable. The version is tested with PHP5.5 3 | 4 | # Documentation 5 | In this section I describe a class and methods you should use 6 | 7 | ## Configuration 8 | To run test you must insert you login, password and appkey to Config/config.ini file. 9 | 10 | `````ini 11 | login = "Insert your login here" 12 | password = "Insert your password here - never do it in production" 13 | apikey = "Insert your apikey here" 14 | sandbox = false 15 | countryCode = 1 16 | ````` 17 | 18 | The file is use only by tests. 19 | 20 | You can create your access data by http://allegro.pl/myaccount/webapi.php/ page. 21 | *sandbox* is use to switch allegro sandbox and production version. *countryCode* is used to select current country. Default 1 mean *Poland*. If you want you can change country to communication with other allegroGroup services like: aukro.cz, molotok.ru, aukro.ua, teszvesz.hu 22 | 23 | ## Security 24 | 25 | Never storage your plain password. Your app should always hash it by sha256 (used by allegro) 26 | 27 | `````php 28 | $hashPassword = base64_encode(hash('sha256', YOUR_ALLEGRO_PASSWORD, true)); 29 | ````` 30 | 31 | ## Class AllegroApi 32 | AllegroApi is main class. Provide api access interface. 33 | 34 | ### Constructor 35 | *Require* one object with login, hashPassword (or password - strongly no recomended), apikey, sandbox, countryCode fields. The data is use to init connections. 36 | 37 | ### LoginEnc 38 | Login to allegro (use constructor data) 39 | 40 | ### Run functions 41 | The api use _call to maping functions names to allegro request. You should use short names without "do". Use getCountries (to rum allegro doGetCountries function). 42 | 43 | `````php 44 | $allegroApi = new AllegroApi($config); 45 | $countrisResponse = $allegroApi->getCountries(); 46 | ````` 47 | 48 | #Allegro functions documentation 49 | Description of all allegro functions are available on http://allegro.pl/webapi/documentation.php 50 | 51 | # Quick start 52 | 53 | Clone empty project: 54 | ~~~bash 55 | git clone https://github.com/SebastianPozoga/PHP-AllegroApi-EmptyProject.git 56 | composer install 57 | ~~~ 58 | 59 | And run: 60 | ~~~ 61 | php index.php 62 | ~~~ 63 | 64 | # Tests 65 | 66 | Run tests by: 67 | ~~~ 68 | phpunit tests/AllegroApiTest 69 | ~~~ 70 | 71 | # Older version 72 | It is strong recomended to use new version of PHP Allegro Api Library. If you must use old version is available on https://github.com/SebastianPozoga/Allegro-PHP-API-14 73 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sebastianpozoga/php-allegroapi", 3 | "description": "A newest library to communicate with Allegro Api. The library is OOP, good tested and easy to use. Make your job enjoyable. The version is tested with PHP5.5", 4 | "keywords": [ 5 | "php", 6 | "allegro", 7 | "api", 8 | "web-api" 9 | ], 10 | "homepage": "https://github.com/sebastianpozoga/php-allegroapi", 11 | "type": "library", 12 | "license": "MIT", 13 | "authors": [ 14 | { 15 | "name": "Sebastian Pożoga", 16 | "email": "sebastian@pozoga.eu" 17 | } 18 | ], 19 | "minimum-stability": "stable", 20 | "require": { 21 | "php": ">=5.4" 22 | }, 23 | "autoload": { 24 | "psr-0": { 25 | "AllegroApi": "src/" 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /config/config.example.ini: -------------------------------------------------------------------------------- 1 | login = "Insert your login here" 2 | password = "Insert your password here - never do it with production" 3 | apikey = "Insert your apikey here" 4 | sandbox = false 5 | countryCode = 1 6 | -------------------------------------------------------------------------------- /src/AllegroApi/AllegroApi.php: -------------------------------------------------------------------------------- 1 | password && !$config->hashPassword) { 27 | throw new AllegroApiException( 28 | "Must set password for Allegro API server", AllegroApiException::PARAMETER_INCORECT 29 | ); 30 | } 31 | if (!$config->login) { 32 | throw new AllegroApiException( 33 | "Must set login for Allegro API server", AllegroApiException::PARAMETER_INCORECT 34 | ); 35 | } 36 | if (!$config->apikey) { 37 | throw new AllegroApiException( 38 | "Must set apikey for Allegro API server", AllegroApiException::PARAMETER_INCORECT 39 | ); 40 | } 41 | if (!isset($config->sandbox)) { 42 | throw new AllegroApiException( 43 | "Must set sandbox flag for Allegro API server", AllegroApiException::PARAMETER_INCORECT 44 | ); 45 | } 46 | if (!$config->countryCode) { 47 | throw new AllegroApiException( 48 | "Must set countryCode for Allegro API server", AllegroApiException::PARAMETER_INCORECT 49 | ); 50 | } 51 | 52 | //save data 53 | $this->config = $config; 54 | 55 | //math wsdl 56 | $wsdl = (isset($config->sandbox) && (int)$config->sandbox) ? self::WSDL_SANDBOX : self::WSDL; 57 | 58 | //crete client 59 | $this->client = new \SoapClient( 60 | $wsdl, array( 61 | 'features' => SOAP_SINGLE_ELEMENT_ARRAYS 62 | ) 63 | ); 64 | 65 | //create request id data 66 | $this->request = array( 67 | 'countryId' => $config->countryCode, //for old function - example: doGetShipmentData 68 | 'countryCode' => $config->countryCode, //for new function 69 | 'webapiKey' => $config->apikey, 70 | 'localVersion' => $this->loadVersionKey($config->countryCode) 71 | ); 72 | } 73 | 74 | /** 75 | * This function is not safe. Always hash password before use and STORAGE it. 76 | * 77 | * @deprecated 78 | */ 79 | function login() { 80 | //always safe login method (hash password if is not hashed) 81 | if (!isset($this->config->hashPassword)) { 82 | //prevents 83 | if (!$this->config->password) { 84 | throw new AllegroApiException("No set password to login"); 85 | } 86 | //do 87 | $this->config->hashPassword = base64_encode(hash('sha256', $this->config->password, true)); 88 | } 89 | $this->loginEnc(); 90 | } 91 | 92 | function loginEnc() { 93 | //prevents 94 | if (!$this->config->hashPassword) { 95 | throw new AllegroApiException("No set sha256 hash password to login"); 96 | } 97 | 98 | //login enc 99 | $request = $this->buildRequest( 100 | array( 101 | 'userLogin' => $this->config->login, 102 | 'userHashPassword' => $this->config->hashPassword 103 | ) 104 | ); 105 | 106 | //add session to request 107 | $this->session = $this->client->doLoginEnc($request); 108 | $this->request['sessionId'] = $this->session->sessionHandlePart; //for new functions 109 | $this->request['sessionHandle'] = $this->session->sessionHandlePart; //for older functions 110 | } 111 | 112 | protected function buildRequest($data) { 113 | return array_replace_recursive($this->request, (array)$data); 114 | } 115 | 116 | protected function buildResponse($obj) { 117 | return $obj; 118 | } 119 | 120 | /* 121 | * MAGIC METHODS 122 | */ 123 | 124 | function __call($name, $arguments) { 125 | //prepare data 126 | $params = isset($arguments[0]) ? (array)$arguments[0] : array(); 127 | $request = $this->buildRequest($params); 128 | 129 | //add 'do' to short function name 130 | $fname = 'do' . ucfirst($name); 131 | 132 | //call SOAP function 133 | $responseData = $this->client->$fname($request); 134 | return $this->buildResponse($responseData); 135 | } 136 | 137 | /* 138 | * HELPERS 139 | */ 140 | 141 | private function loadVersionKey($countryCode) { 142 | $sys = $this->client->doQueryAllSysStatus( 143 | array( 144 | 'countryId' => $this->config->countryCode, 145 | 'webapiKey' => $this->config->apikey 146 | ) 147 | ); 148 | foreach ($sys->sysCountryStatus->item as $row) { 149 | if ($row->countryId == $countryCode) { 150 | return $row->verKey; 151 | } 152 | } 153 | throw new Exception("No find country by code: ${$countryCode}"); 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /src/AllegroApi/AllegroApiException.php: -------------------------------------------------------------------------------- 1 | password = isset($config['password']) ? $config['password'] : null; 18 | $this->hashPassword = isset($config['hashPassword']) ? $config['hashPassword'] : null; 19 | $this->login = $config['login']; 20 | $this->apikey = $config['apikey']; 21 | $this->countryCode = $config['countryCode']; 22 | $this->sandbox = $config['sandbox']; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/AllegroApiTest.php: -------------------------------------------------------------------------------- 1 | getConfig(); 15 | 16 | //create object 17 | $allegroApi = new AllegroApi($config); 18 | 19 | //test plain login 20 | // ok, if no exception 21 | $throwException = false; 22 | try { 23 | $allegroApi->login(); 24 | } catch (Exception $ex) { 25 | $throwException = true; 26 | } 27 | //no throw a exception if success 28 | $this->assertEquals(false, $throwException); 29 | } 30 | 31 | public function testCanLoginEnc() { 32 | //config 33 | $config = ConfigDistributor::getInstance()->getConfig(); 34 | 35 | //create object 36 | $allegroApi = new AllegroApi($config); 37 | 38 | //test plain login 39 | // ok, if no exception 40 | $throwException = false; 41 | try { 42 | $allegroApi->loginEnc(); 43 | } catch (Exception $ex) { 44 | $throwException = true; 45 | } 46 | //no throw a exception if success 47 | $this->assertEquals(false, $throwException); 48 | } 49 | 50 | public function testConstructorPreventArray() { 51 | try { 52 | new AllegroApi([]); 53 | } catch (AllegroApiException $ex) { 54 | //thrown no object exception 55 | $this->assertEquals(AllegroApiException::ALLOW_ONLY_OBJECT, $ex->getCode()); 56 | $throwException = true; 57 | } 58 | // must throw exception 59 | $this->assertEquals(true, $throwException); 60 | } 61 | 62 | public function testCanExecuteRemoteFunction() { 63 | //config 64 | $config = ConfigDistributor::getInstance()->getConfig(); 65 | 66 | //create object 67 | $allegroApi = new AllegroApi($config); 68 | 69 | //execute remote function 70 | $throwException = false; 71 | try { 72 | $allegroApi->getCountries(); 73 | } catch (Exception $ex) { 74 | $throwException = true; 75 | } 76 | //no throw a exception if success 77 | $this->assertEquals(false, $throwException); 78 | } 79 | 80 | public function testDoesExecuteResultIsObject() { 81 | //config 82 | $config = ConfigDistributor::getInstance()->getConfig(); 83 | 84 | //create object 85 | $allegroApi = new AllegroApi($config); 86 | 87 | //execute remote function 88 | $result = $allegroApi->getCountries(); 89 | $this->assertEquals(true, is_object($result)); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /tests/ConfigDistributor.php: -------------------------------------------------------------------------------- 1 | config = json_decode(json_encode($data), FALSE); 19 | 20 | if (!$this->config->login) { 21 | throw new \Exception("Must set login for Allegro API server"); 22 | } 23 | if (!$this->config->password) { 24 | throw new \Exception("Must set password for Allegro API server"); 25 | } 26 | if (!$this->config->apikey) { 27 | throw new \Exception("Must set apikey for Allegro API server"); 28 | } 29 | if (!isset($this->config->sandbox)) { 30 | throw new \Exception("Must set sandbox flag for Allegro API server"); 31 | } 32 | if (!$this->config->countryCode) { 33 | throw new \Exception("Must set countryCode for Allegro API server"); 34 | } 35 | } 36 | 37 | public function getConfig() { 38 | return $this->config; 39 | } 40 | 41 | public function getParameters() { 42 | return array( 43 | 'username' => $this->config->username, 44 | 'password' => $this->config->password, 45 | 'sandbox' => $this->config->sandbox, 46 | 'appkey' => $this->config->appkey, 47 | 'countryCode' => $this->config->countryCode 48 | ); 49 | } 50 | 51 | 52 | /* 53 | * Singleton 54 | */ 55 | 56 | /** 57 | * @return ConfigDistributor 58 | */ 59 | public static function getInstance() { 60 | if (self::$instance == null) { 61 | self::$instance = new ConfigDistributor(); 62 | } 63 | return self::$instance; 64 | } 65 | } 66 | --------------------------------------------------------------------------------