|
| 1 | +# Response |
| 2 | + |
| 3 | +## Object |
| 4 | + |
| 5 | +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: |
| 6 | + |
| 7 | + $response->body() : string; |
| 8 | + $response->json() : array; |
| 9 | + $response->status() : int; |
| 10 | + $response->ok() : bool; |
| 11 | + $response->successful() : bool; |
| 12 | + $response->serverError() : bool; |
| 13 | + $response->clientError() : bool; |
| 14 | + $response->onError(callable $callback): \CodeDredd\Soap\Client\Response; |
| 15 | + $response->collect(): \Illuminate\Support\Collection; |
| 16 | + |
| 17 | +The `CodeDredd\Soap\Client\Response` object also implements the PHP `ArrayAccess` interface, allowing you to access your response data directly on the response: |
| 18 | + |
| 19 | + return Soap::baseWsdl('http://test.com'/v1?wsdl)->call('Get_Users')['name']; |
| 20 | + |
| 21 | +## Error Handling |
| 22 | + |
| 23 | +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: |
| 24 | + |
| 25 | + // Determine if the status code was >= 200 and < 300... |
| 26 | + $response->successful(); |
| 27 | + |
| 28 | + // Determine if the response has a 400 level status code... |
| 29 | + $response->clientError(); |
| 30 | + |
| 31 | + // Determine if the response has a 500 level status code... |
| 32 | + $response->serverError(); |
| 33 | + |
| 34 | +### Throwing Exceptions |
| 35 | + |
| 36 | +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: |
| 37 | + |
| 38 | + $response = Soap::baseWsdl(...)->call(...); |
| 39 | + |
| 40 | + // Throw an exception if a client or server error occurred... |
| 41 | + $response->throw(); |
| 42 | + |
| 43 | + return $response['user']['id']; |
| 44 | + |
| 45 | +The `CodeDredd\Soap\Exceptions\RequestException` instance has a public `$response` property which will allow you to inspect the returned response. |
| 46 | + |
| 47 | +The `throw` method returns the response instance if no error occurred, allowing you to chain other operations onto the `throw` method: |
| 48 | + |
| 49 | + return Soap::baseWsdl(...) |
| 50 | + ->call(...) |
| 51 | + ->throw() |
| 52 | + ->json(); |
0 commit comments