|
2 | 2 |
|
3 | 3 | [](LICENSE.md) |
4 | 4 | [](https://packagist.org/packages/codedredd/laravel-soap) |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | -- [Installation](#installation) |
10 | | -- [Introduction](#introduction) |
11 | | -- [Making Requests](#making-requests) |
12 | | - - [Request Data](#request-data) |
13 | | - - [Headers](#headers) |
14 | | - - [Authentication](#authentication) |
15 | | - - [Error Handling](#error-handling) |
16 | | - - [SOAP Options](#soap-options) |
17 | | -- [Testing](#testing) |
18 | | - - [Faking Responses](#faking-responses) |
19 | | - - [Inspecting Requests](#inspecting-requests) |
20 | | -- [Contributing](#contributing) |
21 | | -- [License](#license) |
| 5 | +[](https://github.com/CodeDredd/laravel-soap/actions?query=workflow%3Atest) |
| 6 | +[](https://github.styleci.io/repos/257192373) |
| 7 | +[](https://github.com/CodeDredd/laravel-soap/releases) |
| 8 | +[](https://codedredd.github.io/laravel-soap/) |
22 | 9 |
|
23 | 10 | <a name="installation"></a> |
24 | 11 | ## Installation |
25 | 12 |
|
26 | 13 | Execute the following command to get the latest version of the package: |
27 | 14 |
|
28 | 15 | composer require codedredd/laravel-soap |
29 | | - |
30 | | -Publish Configuration |
31 | | - |
32 | | - php artisan vendor:publish --provider "CodeDredd\Soap\SoapServiceProvider" |
33 | | - |
34 | | -<a name="introduction"></a> |
35 | | -## Introduction |
36 | | - |
37 | | -This package provides an expressive, minimal API around the [Soap Client from Phpro](https://github.com/phpro/soap-client), allowing you to quickly make outgoing SOAP requests to communicate with other web applications. |
38 | | -It is using [HTTPplug](http://httplug.io/) as handler with [Guzzle](https://github.com/php-http/guzzle6-adapter) as client. |
39 | | -Some code is based/copied on/from [Laravel Http wrapper](https://github.com/illuminate/http). Thanks for inspiration :-) |
40 | | - |
41 | | -<a name="making-requests"></a> |
42 | | -## Making Requests |
43 | | - |
44 | | -To make requests, you may use the `call` method or your soap action through magic `__call`. First, let's examine how to make a basic `action` request: |
45 | | - |
46 | | - use CodeDredd\Soap\Facades\Soap; |
47 | | - |
48 | | - $response = Soap::baseWsdl('http://test.com'/v1?wsdl)->call('Get_Users'); |
49 | | - // Or via magic method call |
50 | | - $response = Soap::baseWsdl('http://test.com'/v1?wsdl)->Get_Users(); |
51 | | - |
52 | | -The `call` method returns an instance of `CodeDredd\Soap\Client\Response`, which provides a variety of methods that may be used to inspect the response: |
53 | | - |
54 | | - $response->body() : string; |
55 | | - $response->json() : array; |
56 | | - $response->status() : int; |
57 | | - $response->ok() : bool; |
58 | | - $response->successful() : bool; |
59 | | - $response->serverError() : bool; |
60 | | - $response->clientError() : bool; |
61 | | - |
62 | | -The `CodeDredd\Soap\Client\Response` object also implements the PHP `ArrayAccess` interface, allowing you to access your response data directly on the response: |
63 | | - |
64 | | - return Soap::baseWsdl('http://test.com'/v1?wsdl)->call('Get_Users')['name']; |
65 | 16 |
|
66 | | -If you have published the config file then your able to setup a soap client in that configuration. After that it's even easier to initialize the client: |
67 | | - |
68 | | - $client = Soap::buildClient('your_client_name'); |
69 | | - $response = $client->call(...); |
70 | | - |
71 | | -<a name="request-data"></a> |
72 | | -### Request Data |
73 | | - |
74 | | -Of course, calling a action with arguments is also possible: |
75 | | - |
76 | | - $response = Soap::baseWsdl('http://test.com'/v1?wsdl) |
77 | | - ->call('Submit_User', [ |
78 | | - 'name' => 'Steve', |
79 | | - 'role' => 'Network Administrator', |
80 | | - ]); |
81 | | - // Or via magic method call |
82 | | - $response = Soap::baseWsdl('http://test.com'/v1?wsdl) |
83 | | - ->Submit_User([ |
84 | | - 'name' => 'Steve', |
85 | | - 'role' => 'Network Administrator', |
86 | | - ]); |
87 | | - |
88 | | -<a name="headers"></a> |
89 | | -### Headers |
90 | | - |
91 | | -Headers may be added to requests using the `withHeaders` method. This `withHeaders` method accepts an array of key / value pairs: |
92 | | - |
93 | | - $response = Soap::withHeaders([ |
94 | | - 'X-First' => 'foo', |
95 | | - 'X-Second' => 'bar' |
96 | | - ])->baseWsdl('http://test.com'/v1?wsdl)->call('Get_Users'); |
97 | | - |
98 | | -<a name="authentication"></a> |
99 | | -### Authentication |
100 | | - |
101 | | -You may specify basic authentication credentials using the `withBasicAuth` method, respectively: |
102 | | - |
103 | | - // Basic authentication... |
104 | | - $response = Soap::baseWsdl('http://test.com'/v1?wsdl)->withBasicAuth('[email protected]', 'secret')->call(...); |
105 | | - |
106 | | -#### Web Service Security (WSS / WSSE) |
107 | | - |
108 | | -Internally it is using the [wse-php package of robrichards](https://github.com/robrichards/wse-php) which is a well known library that is used by many developers. |
109 | | -It also supports not secured Wsse but with token: |
110 | | - |
111 | | - //Not secure |
112 | | - $response = Soap::baseWsdl('http://test.com'/v1?wsdl)->withWsse([ |
113 | | - 'userTokenName' => 'username', |
114 | | - 'userTokenPassword' => 'password', |
115 | | - ])->call(...); |
116 | | - //Secure |
117 | | - $response = Soap::baseWsdl('http://test.com'/v1?wsdl)->withWsse([ |
118 | | - 'privateKeyFile' => 'path/to/privatekey.pem', |
119 | | - 'publicKeyFile' => 'path/to/publickey.pyb', |
120 | | - ])->call(...); |
121 | | - |
122 | | -You have following Wsse Options: |
123 | | - |
124 | | - 'userTokenName' : string |
125 | | - 'userTokenPassword' : string |
126 | | - 'privateKeyFile' : string |
127 | | - 'publicKeyFile' : string |
128 | | - 'serverCertificateFile' : string |
129 | | - 'serverCertificateHasSubjectKeyIdentifier' : boolean |
130 | | - 'userTokenDigest' : boolean |
131 | | - 'digitalSignMethod' : string |
132 | | - 'timestamp' : integer |
133 | | - 'signAllHeaders' => : boolean |
134 | | - |
135 | | -#### Web Service Addressing (WSA) |
136 | | - |
137 | | -Like Wss/Wsse it uses the same package: |
138 | | - |
139 | | - $response = Soap::baseWsdl(...) |
140 | | - ->withWsa() |
141 | | - ->call(...) |
142 | | - |
143 | | -<a name="error-handling"></a> |
144 | | -### Error Handling |
145 | | - |
146 | | -Unlike Guzzle's default behavior, this SOAP client wrapper does not throw exceptions on client or server errors (`400` and `500` level responses from servers). You may determine if one of these errors was returned using the `successful`, `clientError`, or `serverError` methods: |
147 | | - |
148 | | - // Determine if the status code was >= 200 and < 300... |
149 | | - $response->successful(); |
150 | | - |
151 | | - // Determine if the response has a 400 level status code... |
152 | | - $response->clientError(); |
153 | | - |
154 | | - // Determine if the response has a 500 level status code... |
155 | | - $response->serverError(); |
156 | | - |
157 | | -#### Throwing Exceptions |
158 | | - |
159 | | -If you have a response instance and would like to throw an instance of `CodeDredd\Soap\Exceptions\RequestException` if the response is a client or server error, you may use the `throw` method: |
160 | | - |
161 | | - $response = Soap::baseWsdl(...)->call(...); |
162 | | - |
163 | | - // Throw an exception if a client or server error occurred... |
164 | | - $response->throw(); |
165 | | - |
166 | | - return $response['user']['id']; |
167 | | - |
168 | | -The `CodeDredd\Soap\Exceptions\RequestException` instance has a public `$response` property which will allow you to inspect the returned response. |
169 | | - |
170 | | -The `throw` method returns the response instance if no error occurred, allowing you to chain other operations onto the `throw` method: |
171 | | - |
172 | | - return Soap::baseWsdl(...) |
173 | | - ->call(...) |
174 | | - ->throw() |
175 | | - ->json(); |
176 | | - |
177 | | -<a name="soap-options"></a> |
178 | | -### Soap Client Options |
179 | | - |
180 | | -You may specify additional [Soap request options](https://doc.bccnsoft.com/docs/php-docs-7-en/soapclient.soapclient.html) using the `withOptions` method. The `withOptions` method accepts an array of key / value pairs: |
181 | | - |
182 | | - $response = Soap::baseWsdl(...)->withOptions([ |
183 | | - 'trace' => true, |
184 | | - ])->call(...); |
185 | | - |
186 | | -By default this options are set by the Phpro package: |
187 | | - |
188 | | - 'trace' => true, |
189 | | - 'exceptions' => true, |
190 | | - 'keep_alive' => true, |
191 | | - 'cache_wsdl' => WSDL_CACHE_DISK, // Avoid memory cache: this causes SegFaults from time to time. |
192 | | - 'features' => SOAP_SINGLE_ELEMENT_ARRAYS, |
193 | | - 'typemap' => new TypeConverterCollection([ |
194 | | - new TypeConverter\DateTimeTypeConverter(), |
195 | | - new TypeConverter\DateTypeConverter(), |
196 | | - new TypeConverter\DecimalTypeConverter(), |
197 | | - new TypeConverter\DoubleTypeConverter() |
198 | | - ]), |
199 | | - |
200 | | -<a name="testing"></a> |
201 | | -## Testing |
202 | | - |
203 | | -Many Laravel services provide functionality to help you easily and expressively write tests, and this SOAP wrapper is no exception. The `Soap` facade's `fake` method allows you to instruct the SOAP client to return stubbed / dummy responses when requests are made. |
204 | | - |
205 | | -<a name="faking-responses"></a> |
206 | | -### Faking Responses |
207 | | - |
208 | | -For example, to instruct the SOAP client to return empty, `200` status code responses for every request, you may call the `fake` method with no arguments: |
209 | | - |
210 | | - use CodeDredd\Soap\Facades\Soap; |
211 | | - |
212 | | - Soap::fake(); |
213 | | - |
214 | | - $response = Soap::baseWsdl(...)->call(...); |
215 | | - |
216 | | -#### Faking Specific URLs |
217 | | - |
218 | | -Alternatively, you may pass an array to the `fake` method. The array's keys should represent ACTION patterns that you wish to fake and their associated responses. The `*` character may be used as a wildcard character. You may use the `response` method to construct stub / fake responses for these endpoints: |
219 | | -The difference between Laravels HTTP wrapper is the fact that actions which are not defined in fake are also faked with a default 200 response! |
220 | | -Also a faked response status code is always 200 if you define it in the range between 200 and 400. Status codes 400 and greater are correct responded. |
221 | | - |
222 | | - Soap::fake([ |
223 | | - // Stub a JSON response for all Get_ actions... |
224 | | - 'Get_*' => Soap::response(['foo' => 'bar'], 200, ['Headers']), |
225 | | - |
226 | | - // Stub a string response for Submit_User action |
227 | | - 'Submit_User' => Soap::response('Hello World', 200, ['Headers']), |
228 | | - ]); |
229 | | - |
230 | | -If you would like to overwrite the fallback ACTION pattern that will stub all unmatched URLs, you may use a single `*` character: |
231 | | - |
232 | | - Soap::fake([ |
233 | | - // Stub a JSON response for all Get_ actions... |
234 | | - 'Get_*' => Soap::response(['foo' => 'bar'], 200, ['Headers']), |
235 | | - |
236 | | - // Stub a string response for all other actions |
237 | | - '*' => Soap::response('Hello World', 200, ['Headers']), |
238 | | - ]); |
239 | | - |
240 | | -One important notice. Because a SOAP API doesn't return only string every response with only a string in the body will be formatted to an array: |
241 | | - |
242 | | - //For above example |
243 | | - [ |
244 | | - 'response' => 'Hello World' |
245 | | - ] |
246 | | - |
247 | | -#### Faking Response Sequences |
248 | | - |
249 | | -Sometimes you may need to specify that a single ACTION should return a series of fake responses in a specific order. You may accomplish this by using the `Soap::sequence` method to build the responses: |
250 | | - |
251 | | - Soap::fake([ |
252 | | - // Stub a series of responses for Get_* actions... |
253 | | - 'Get_*' => Soap::sequence() |
254 | | - ->push('Hello World') |
255 | | - ->push(['foo' => 'bar']) |
256 | | - ->pushStatus(404) |
257 | | - ]); |
258 | | - |
259 | | -When all of the responses in a response sequence have been consumed, any further requests will cause the response sequence to throw an exception. If you would like to specify a default response that should be returned when a sequence is empty, you may use the `whenEmpty` method: |
260 | | - |
261 | | - Soap::fake([ |
262 | | - // Stub a series of responses for Get_* actions... |
263 | | - 'Get_*' => Soap::sequence() |
264 | | - ->push('Hello World') |
265 | | - ->push(['foo' => 'bar']) |
266 | | - ->whenEmpty(Soap::response()) |
267 | | - ]); |
268 | | - |
269 | | -If you would like to fake a sequence of responses but do not need to specify a specific ACTION pattern that should be faked, you may use the `Soap::fakeSequence` method: |
270 | | - |
271 | | - Soap::fakeSequence() |
272 | | - ->push('Hello World') |
273 | | - ->whenEmpty(Soap::response()); |
274 | | - |
275 | | -#### Fake Callback |
276 | | - |
277 | | -If you require more complicated logic to determine what responses to return for certain endpoints, you may pass a callback to the `fake` method. This callback will receive an instance of `CodeDredd\Soap\Client\Request` and should return a response instance: |
278 | | - |
279 | | - Soap::fake(function ($request) { |
280 | | - return Soap::response('Hello World', 200); |
281 | | - }); |
282 | | - |
283 | | -<a name="inspecting-requests"></a> |
284 | | -### Inspecting Requests |
285 | | - |
286 | | -When faking responses, you may occasionally wish to inspect the requests the client receives in order to make sure your application is sending the correct data or headers. You may accomplish this by calling the `Soap::assertSent` method after calling `Soap::fake`. |
287 | | - |
288 | | -The `assertSent` method accepts a callback which will be given an `CodeDredd\Soap\Client\Request` instance and should return a boolean value indicating if the request matches your expectations. In order for the test to pass, at least one request must have been issued matching the given expectations: |
289 | | - |
290 | | - Soap::fake(); |
291 | | - |
292 | | - Soap::withHeaders([ |
293 | | - 'X-First' => 'foo', |
294 | | - ])->baseWsdl('http://test.com/v1?wsdl') |
295 | | - ->call('Get_Users', [ |
296 | | - 'name' => 'CodeDredd' |
297 | | - ]); |
298 | | - |
299 | | - Soap::assertSent(function ($request) { |
300 | | - return $request->action() === 'Get_Users' && |
301 | | - $request->arguments() === ['name' => 'CodeDredd']; |
302 | | - }); |
303 | | - //Or shortcut |
304 | | - Soap::assertActionSent('Get_Users') |
305 | | - |
306 | | -If needed, you may assert that a specific request was not sent using the `assertNotSent` method: |
307 | | - |
308 | | - Soap::fake(); |
309 | | - |
310 | | - Soap::baseWsdl('http://test.com/v1?wsdl') |
311 | | - ->call('Get_Users'); |
312 | | - |
313 | | - Soap::assertNotSent(function (Request $request) { |
314 | | - return $request->action() === 'Get_Posts'; |
315 | | - }); |
316 | | - |
317 | | -Or, if you would like to assert that no requests were sent, you may use the `assertNothingSent` method: |
318 | | - |
319 | | - Soap::fake(); |
320 | | - |
321 | | - Soap::assertNothingSent(); |
| 17 | +## Documentation |
| 18 | +You can find here a detailed [documentation](https://codedredd.github.io/laravel-soap/) |
322 | 19 |
|
323 | 20 | <a name="contributing"></a> |
324 | 21 | ## Contributing |
|
0 commit comments