|
1 | | -<div align="center"> |
2 | | - <a href="https://openapi.com/"> |
3 | | - <img alt="Openapi SDK for PHP" src=".github/assets/repo-header-a3.png" > |
4 | | - </a> |
| 1 | +# OpenAPI PHP SDK |
5 | 2 |
|
6 | | - <h1>Openapi® client for Rust</h1> |
7 | | - <h4>The perfect starting point to integrate <a href="https://openapi.com/">Openapi®</a> within your Rust project</h4> |
| 3 | +A minimal and agnostic PHP SDK for OpenAPI, inspired by the Rust implementation. This SDK provides only the core HTTP primitives needed to interact with any OpenAPI service. |
8 | 4 |
|
9 | | -[](https://github.com/<username>/<repo>/actions) |
10 | | -[](https://crates.io/crates/<crate_name>) |
11 | | -[](https://docs.rs/<crate_name>) |
12 | | -[](LICENSE) |
13 | | -[](https://www.rust-lang.org/) |
14 | | -</div> |
| 5 | +## Features |
15 | 6 |
|
16 | | -# OpenAPI Library |
| 7 | +- **Agnostic Design**: No API-specific classes, works with any OpenAPI service |
| 8 | +- **Minimal Dependencies**: Only requires PHP 8.0+ and cURL |
| 9 | +- **OAuth Support**: Built-in OAuth client for token management |
| 10 | +- **HTTP Primitives**: GET, POST, PUT, DELETE, PATCH methods |
| 11 | +- **Clean Interface**: Similar to the Rust SDK design |
17 | 12 |
|
18 | | -<!-- vscode-markdown-toc --> |
19 | | -* 1. [Installation](#Installation) |
20 | | -* 2. [Usage](#Usage) |
21 | | - * 2.1. [Instanza della classe](#Instanzadellaclasse) |
22 | | - * 2.2. [Esempi](#Esempi) |
23 | | -* 3. [Modulo comuni](#Modulocomuni) |
24 | | - * 3.1. [Esempi](#Esempi-1) |
25 | | -* 4. [Modulo imprese](#Moduloimprese) |
26 | | - * 4.1. [Utilizzo](#Utilizzo) |
27 | | - * 4.2. [Esempi](#Esempi-1) |
28 | | -* 5. [Modulo Marche Temporali](#ModuloMarcheTemporali) |
29 | | - * 5.1. [Esempi](#Esempi-1) |
30 | | -* 6. [Modulo SMS](#ModuloSMS) |
31 | | - * 6.1. [Inviare un SMS](#InviareunSMS) |
32 | | -* 7. [Modulo Visengine](#ModuloVisengine) |
| 13 | +## Installation |
33 | 14 |
|
34 | | -<!-- vscode-markdown-toc-config |
35 | | - numbering=true |
36 | | - autoSave=true |
37 | | - /vscode-markdown-toc-config --> |
38 | | -<!-- /vscode-markdown-toc --> |
39 | | - |
40 | | - |
41 | | -## 1. <a name='Installation'></a>Installation |
42 | | - |
43 | | -```sh |
44 | | -composer require altravia/openapi |
45 | | -``` |
46 | | - |
47 | | -## 2. <a name='Usage'></a>Usage |
48 | | - |
49 | | -### 2.1. <a name='Instanzadellaclasse'></a>Instanza della classe |
50 | | - |
51 | | -```php |
52 | | -require_once 'vendor/autoload.php'; |
53 | | - |
54 | | -$openapi = new \OpenApi\OpenApi($scopes, $user, $apikey, $environment); |
55 | | -``` |
56 | | - |
57 | | -Dove `$scopes` è un array di stringhe o di oggetti in uno dei seguenti formati: |
58 | | - |
59 | | -```php |
60 | | -$scopes = [ |
61 | | - "GET:ws.ufficiopostale.com/comuni", |
62 | | - [ |
63 | | - "domain"=>"ws.ufficiopostale.com", |
64 | | - "method"=>"comuni", |
65 | | - "mode" =>"GET" |
66 | | - ] |
67 | | -]; |
| 15 | +```bash |
| 16 | +composer require openapi/openapi-sdk |
68 | 17 | ``` |
69 | 18 |
|
70 | | -...e `$environment` è l'ambiente sceltro tra `'test'` (default) e `'production'` |
71 | | - |
72 | | -OpenApi si occuperá di reperire automaticamente, o generare, un nuovo token quando necessario. |
| 19 | +## Quick Start |
73 | 20 |
|
74 | | -A questo punto, in base agli scopes indicati vengono creati i seguenti oggetti: |
75 | | - |
76 | | - |
77 | | -```php |
78 | | -// Ogni oggetto verrá creato solo se disponibile nello scope. |
79 | | -$openapi->ufficiopostale; |
80 | | -$openapi->comuni; |
81 | | -$openapi->imprese; |
82 | | -$openapi->visengine; |
83 | | -$openapi->marcheTemporali; |
84 | | -$openapi->geocoding; |
85 | | -$openapi->SMS; |
86 | | -$openapi->firmaDigitale; |
87 | | -$openapi->pecMassiva; |
88 | | -``` |
89 | | -che possono essere usati al seguente modo: |
| 21 | +### Token Generation |
90 | 22 |
|
91 | 23 | ```php |
92 | | -$this->openapi->ufficioposale->getCitiesByCap('00132'); |
93 | | -``` |
94 | | -### 2.2. <a name='Esempi'></a>Esempi |
| 24 | +use OpenApi\OauthClient; |
95 | 25 |
|
96 | | -```php |
97 | | -require_once 'vendor/autoload.php'; |
| 26 | +$oauthClient = new OauthClient('username', 'apikey', true); // true for test environment |
98 | 27 |
|
99 | | -// Dichiaro gli scopes necessari |
100 | 28 | $scopes = [ |
101 | | - 'GET:comuni.openapi.it/cap', |
102 | | - 'GET:imprese.altravia.com/advance', |
| 29 | + 'GET:test.imprese.openapi.it/advance', |
| 30 | + 'POST:test.postontarget.com/fields/country' |
103 | 31 | ]; |
104 | 32 |
|
105 | | -$openapi = new OpenApi\OpenApi($scopes, 'my_username','my_api_key', 'test'); |
106 | | - |
107 | | -// Comuni: prendi informazioni sul cap 00132 |
108 | | -$cap = $openapi->comuni->getCitiesByCap('00132'); |
109 | | - |
110 | | -// Imprese: prendi informazioni su una specifica impresa |
111 | | -$impresa = $openapi->imprese->getByPartitaIva('12485671007'); |
112 | | - |
113 | | -// Ufficio Postale: ottieni informaizoni sul tracking |
114 | | -$track = $this->openapi->ufficiopostale->track('123456789'); |
| 33 | +$result = $oauthClient->createToken($scopes, 3600); |
| 34 | +$tokenData = json_decode($result, true); |
| 35 | +$token = $tokenData['token']; |
115 | 36 | ``` |
116 | 37 |
|
117 | | - |
118 | | -## 3. <a name='Modulocomuni'></a>Modulo comuni |
119 | | -Consente di prendere informazioni su comuni e provincie. |
120 | | - |
121 | | -* `getCitiesByCap` |
122 | | -* `getComuneByCatasto` |
123 | | -* `getRegioni` |
124 | | -* `getProvince` |
125 | | -* `getComuni` |
126 | | - |
127 | | -### 3.1. <a name='Esempi-1'></a>Esempi |
| 38 | +### Making API Calls |
128 | 39 |
|
129 | 40 | ```php |
130 | | -$provincia = 'RM'; |
131 | | -$comuni = $this->openapi->comuni->getComuni($provincia); |
| 41 | +use OpenApi\Client; |
132 | 42 |
|
133 | | -var_dump($comuni['comuni']); |
134 | | -/* |
| 43 | +$client = new Client($token); |
135 | 44 |
|
136 | | -["nome_provincia"]=> |
137 | | - string(4) "Roma" |
138 | | - ["sigla_provincia"]=> |
139 | | - string(2) "RM" |
140 | | - ["regione"]=> |
141 | | - string(5) "Lazio" |
142 | | - ["comuni"]=> |
143 | | - array(121) { |
144 | | - [0]=> |
145 | | - string(6) "Affile" |
146 | | - ... |
147 | | -*/ |
| 45 | +// GET request |
| 46 | +$params = ['denominazione' => 'altravia', 'provincia' => 'RM']; |
| 47 | +$response = $client->get('https://test.imprese.openapi.it/advance', $params); |
148 | 48 |
|
| 49 | +// POST request |
| 50 | +$payload = ['limit' => 10, 'query' => ['country_code' => 'IT']]; |
| 51 | +$response = $client->post('https://test.postontarget.com/fields/country', $payload); |
149 | 52 |
|
| 53 | +// Other HTTP methods |
| 54 | +$response = $client->put($url, $payload); |
| 55 | +$response = $client->delete($url); |
| 56 | +$response = $client->patch($url, $payload); |
150 | 57 | ``` |
151 | 58 |
|
152 | | -## 4. <a name='Moduloimprese'></a>Modulo imprese |
153 | | -### 4.1. <a name='Utilizzo'></a>Utilizzo |
154 | | -Il modulo imprese espone i seguenti metodi: |
155 | | -* `getByPartitaIva` |
156 | | -* `getClosed` |
157 | | -* `getVatGroup` |
158 | | -* `getPec` |
159 | | -* `getBySearch` |
| 59 | +## Architecture |
160 | 60 |
|
161 | | -Per `getBySearch` e `getByPartitaIva` è richiesto accesso allo scope `/advance` |
| 61 | +This SDK follows a minimal approach with only essential components: |
162 | 62 |
|
163 | | -### 4.2. <a name='Esempi-1'></a>Esempi |
164 | | -Utilizziamo `getBySearch` per cercare un'azienda il cui nome inizia con `Altrav` a Roma |
165 | | - |
166 | | -```php |
167 | | -$autocomplete = $this->openapi->imprese->getBySearch('Altrav*', 'RM'); |
168 | | - |
169 | | -/* |
170 | | - [0]=> |
171 | | - object(stdClass)#41 (10) { |
172 | | - ["piva"]=> |
173 | | - string(11) "12485671007" |
174 | | - ["cf"]=> |
175 | | - string(11) "12485671007" |
176 | | - ["denominazione"]=> |
177 | | - string(20) "ALTRAVIA SERVIZI SRL" |
178 | | - [1]=> |
179 | | - object(stdClass)#42 (10) { |
180 | | - ["id"]=> |
181 | | - string(24) "4242424242" |
182 | | - ["denominazione"]=> |
183 | | - string(18) "xxx Altravia Esempio 2" |
184 | | - ... |
185 | | - */ |
186 | | -``` |
| 63 | +- `OauthClient`: Handles OAuth authentication and token management |
| 64 | +- `Client`: Agnostic HTTP client for API calls |
| 65 | +- `Exception`: Error handling |
| 66 | +- `Cache\CacheInterface`: Optional caching interface |
187 | 67 |
|
188 | | -## 5. <a name='ModuloMarcheTemporali'></a>Modulo Marche Temporali |
189 | | -* `availability` |
190 | | -* `checkLotto` |
191 | | -* `purcahse` |
| 68 | +## Requirements |
192 | 69 |
|
193 | | -### 5.1. <a name='Esempi-1'></a>Esempi |
194 | | - |
195 | | -```php |
196 | | -// Controlliamo la disponibilitá di una marca di inforcert o aruba |
197 | | -$disponibilita = $this->openapi->marcheTemporali->availability('infocert', 1); |
198 | | - |
199 | | -// Se le marche sono disponibili, acquistiamone una |
200 | | -if ($disponibilita->availability > 0) { |
201 | | - try { |
202 | | - $marca = $this->openapi->marcheTemporali->purcahse('infocert', 1); |
203 | | - } catch (\OpenApi\classes\exception\OpenApiMarcheTemporaliException $e) { |
204 | | - error_log(var_dump($e)); |
205 | | - } |
206 | | -} |
207 | | -``` |
208 | | - |
209 | | -## 6. <a name='ModuloSMS'></a>Modulo SMS |
210 | | -* `getRecipients` |
211 | | -* `getMessage` |
212 | | -* `sendMore` |
213 | | -* `sendOne` |
214 | | - |
215 | | -### 6.1. <a name='InviareunSMS'></a>Inviare un SMS |
216 | | -Per inviare un SMS, per prima cosa definiamo i destinatari: |
217 | | - |
218 | | -```php |
219 | | -$recipient = '+39-3939989741'; |
220 | | -// OR |
221 | | -$recipients = [ |
222 | | - [ |
223 | | - 'number' => '+39-3939989741', |
224 | | - 'fields' => ['nome' => 'NomeDestinatario'] |
225 | | - ] |
226 | | -]; |
227 | | -``` |
228 | | - |
229 | | -Possiamo ora procedere ad inviare un SMS: |
230 | | -```php |
231 | | - |
232 | | -try { |
233 | | - $priority = 1; |
234 | | - $options = null; |
235 | | - $singleSms = $this->openapi->SMS->sendOne('Nome del mittente', $recipient, 'lorem ipsum', null, $priority, $options); |
236 | | -} catch (\OpenApi\classes\exception\OpenApiConnectionsException $e) { |
237 | | - throw 'Non è stato possibile recapitare il messaggio'; |
238 | | -} |
239 | | -``` |
240 | | - |
241 | | -Possiamo anche speficiare i prefissi in modo indipendente: |
242 | | -```php |
243 | | -$this->openapi->SMS->sendOne('Nome del mittente', '3939989741', 'lorem ipsum', '+42', 1, null); |
244 | | -``` |
245 | | - |
246 | | -O passare delle opzioni |
247 | | -```php |
248 | | -$options = ['timestamp_send' => '2021-04-20'] |
249 | | -$this->openapi->SMS->sendOne('Nome del mittente', '3939989741', 'lorem ipsum', '+42', 1, $options); |
250 | | -``` |
251 | | - |
252 | | -## 7. <a name='ModuloVisengine'></a>Modulo Visengine |
253 | | -Come prima cosa, settiamo l'hash della visura che vogliamo richiedere |
254 | | - |
255 | | -```php |
256 | | -// https://developers.openapi.it/services/visengine |
257 | | -$this->openapi->visengine->setHash($visura->hash); |
258 | | -``` |
259 | | - |
260 | | -A questo punto, possiamo lanciare `createRequest`, che ritornerà una istanza vuota della visura che andremo a creare della struttura richiesta |
261 | | - |
262 | | -```php |
263 | | -$request = $this->openapi->visengine->createRequest(); |
264 | | -``` |
265 | | - |
266 | | -Prodediamo a completare l'oggetto, che potremmo passare a sendRequest quando pronto |
267 | | - |
268 | | -```php |
269 | | -$request->setJson(['$0' => 'abcd', '$1' => '12485671007']); |
270 | | - // url di callback, oggetto con dati aggiuntivi, metodo |
271 | | -$request->setCallbackData('https://example.com', new stdClass(), 'POST'); |
272 | | -$visura = $this->openapi->visengine->sendRequest($request); |
273 | | -``` |
| 70 | +- PHP 8.0 or higher |
| 71 | +- cURL extension |
| 72 | +- JSON extension |
0 commit comments