├── .gitignore ├── LICENSE ├── README.md ├── composer.json └── src └── Artisaninweb └── SoapWrapper ├── Client.php ├── Exceptions ├── ServiceAlreadyExists.php ├── ServiceMethodNotExists.php └── ServiceNotFound.php ├── Facade.php ├── Service.php ├── ServiceProvider.php └── SoapWrapper.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Michael van de Rijt 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 | Laravel SoapClient Wrapper 2 | =========================== 3 | 4 | A SoapClient wrapper integration for Laravel.
5 | Makes it easy to use Soap in a Laravel application.
6 | 7 | Please report any bugs or features here:
8 | https://github.com/artisaninweb/laravel-soap/issues/ 9 | 10 | Installation 11 | ============ 12 | 13 | ## Laravel 14 | 15 | ####Installation for Laravel 5.2 and above: 16 | 17 | Run `composer require artisaninweb/laravel-soap` 18 | 19 | Add the service provider in `app/config/app.php`. 20 | 21 | ```php 22 | Artisaninweb\SoapWrapper\ServiceProvider::class, 23 | ``` 24 | 25 | To use the alias, add this to the aliases in `app/config/app.php`. 26 | 27 | ```php 28 | 'SoapWrapper' => Artisaninweb\SoapWrapper\Facade\SoapWrapper::class, 29 | ``` 30 | 31 | 32 | ####Installation for Laravel 5.1 and below : 33 | 34 | 35 | Add `artisaninweb/laravel-soap` as requirement to composer.json 36 | 37 | ```javascript 38 | { 39 | "require": { 40 | "artisaninweb/laravel-soap": "0.3.*" 41 | } 42 | } 43 | ``` 44 | 45 | > If you're using Laravel 5.5 or higher you can skip the two config setups below. 46 | 47 | Add the service provider in `app/config/app.php`. 48 | 49 | ```php 50 | 'Artisaninweb\SoapWrapper\ServiceProvider' 51 | ``` 52 | 53 | To use the facade add this to the facades in `app/config/app.php`. 54 | 55 | ```php 56 | 'SoapWrapper' => 'Artisaninweb\SoapWrapper\Facade' 57 | ``` 58 | 59 | ## Lumen 60 | 61 | Open `bootstrap/app.php` and register the required service provider: 62 | ```php 63 | $app->register(Artisaninweb\SoapWrapper\ServiceProvider::class); 64 | ``` 65 | 66 | register class alias: 67 | ```php 68 | class_alias('Artisaninweb\SoapWrapper\Facade', 'SoapWrapper'); 69 | ``` 70 | 71 | *Facades must be enabled.* 72 | 73 | 74 | Usage 75 | ============ 76 | 77 | How to add a service to the wrapper and use it. 78 | 79 | ```php 80 | soapWrapper = $soapWrapper; 103 | } 104 | 105 | /** 106 | * Use the SoapWrapper 107 | */ 108 | public function show() 109 | { 110 | $this->soapWrapper->add('Currency', function ($service) { 111 | $service 112 | ->wsdl('http://currencyconverter.kowabunga.net/converter.asmx?WSDL') 113 | ->trace(true) 114 | ->classmap([ 115 | GetConversionAmount::class, 116 | GetConversionAmountResponse::class, 117 | ]); 118 | }); 119 | 120 | // Without classmap 121 | $response = $this->soapWrapper->call('Currency.GetConversionAmount', [ 122 | 'CurrencyFrom' => 'USD', 123 | 'CurrencyTo' => 'EUR', 124 | 'RateDate' => '2014-06-05', 125 | 'Amount' => '1000', 126 | ]); 127 | 128 | var_dump($response); 129 | 130 | // With classmap 131 | $response = $this->soapWrapper->call('Currency.GetConversionAmount', [ 132 | new GetConversionAmount('USD', 'EUR', '2014-06-05', '1000') 133 | ]); 134 | 135 | var_dump($response); 136 | exit; 137 | } 138 | } 139 | ``` 140 | 141 | Service functions 142 | ============ 143 | ```php 144 | $this->soapWrapper->add('Currency', function ($service) { 145 | $service 146 | ->wsdl() // The WSDL url 147 | ->trace(true) // Optional: (parameter: true/false) 148 | ->header() // Optional: (parameters: $namespace,$name,$data,$mustunderstand,$actor) 149 | ->customHeader() // Optional: (parameters: $customerHeader) Use this to add a custom SoapHeader or extended class 150 | ->cookie() // Optional: (parameters: $name,$value) 151 | ->location() // Optional: (parameter: $location) 152 | ->certificate() // Optional: (parameter: $certLocation) 153 | ->cache(WSDL_CACHE_NONE) // Optional: Set the WSDL cache 154 | 155 | // Optional: Set some extra options 156 | ->options([ 157 | 'login' => 'username', 158 | 'password' => 'password' 159 | ]) 160 | 161 | // Optional: Classmap 162 | ->classmap([ 163 | GetConversionAmount::class, 164 | GetConversionAmountResponse::class, 165 | ]); 166 | }); 167 | ``` 168 | 169 | Classmap 170 | ============ 171 | 172 | If you are using classmap you can add folders like for example: 173 | - App\Soap 174 | - App\Soap\Request 175 | - App\Soap\Response 176 | 177 | Request: App\Soap\Request\GetConversionAmount 178 | 179 | ```php 180 | CurrencyFrom = $CurrencyFrom; 217 | $this->CurrencyTo = $CurrencyTo; 218 | $this->RateDate = $RateDate; 219 | $this->Amount = $Amount; 220 | } 221 | 222 | /** 223 | * @return string 224 | */ 225 | public function getCurrencyFrom() 226 | { 227 | return $this->CurrencyFrom; 228 | } 229 | 230 | /** 231 | * @return string 232 | */ 233 | public function getCurrencyTo() 234 | { 235 | return $this->CurrencyTo; 236 | } 237 | 238 | /** 239 | * @return string 240 | */ 241 | public function getRateDate() 242 | { 243 | return $this->RateDate; 244 | } 245 | 246 | /** 247 | * @return string 248 | */ 249 | public function getAmount() 250 | { 251 | return $this->Amount; 252 | } 253 | } 254 | ``` 255 | 256 | Response: App\Soap\Response\GetConversionAmountResponse 257 | 258 | ```php 259 | GetConversionAmountResult = $GetConversionAmountResult; 278 | } 279 | 280 | /** 281 | * @return string 282 | */ 283 | public function getGetConversionAmountResult() 284 | { 285 | return $this->GetConversionAmountResult; 286 | } 287 | } 288 | ``` 289 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "artisaninweb/laravel-soap", 3 | "description": "A SoapClient wrapper integration for Laravel", 4 | "keywords": ["laravel", "soap", "client", "wrapper"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Michael van de Rijt", 9 | "email": "me@mvdrijt.com" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.4.0", 14 | "ext-soap": "*" 15 | }, 16 | "autoload": { 17 | "psr-0": { 18 | "Artisaninweb\\SoapWrapper": "src/" 19 | } 20 | }, 21 | "extra": { 22 | "laravel": { 23 | "providers": [ 24 | "Artisaninweb\\SoapWrapper\\ServiceProvider" 25 | ], 26 | "aliases": { 27 | "SoapWrapper": "Artisaninweb\\SoapWrapper\\Facade" 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Artisaninweb/SoapWrapper/Client.php: -------------------------------------------------------------------------------- 1 | headers($headers); 27 | } 28 | } 29 | 30 | /** 31 | * Get all functions from the service 32 | * 33 | * @return mixed 34 | */ 35 | public function getFunctions() 36 | { 37 | return $this->__getFunctions(); 38 | } 39 | 40 | /** 41 | * Get the last request 42 | * 43 | * @return mixed 44 | */ 45 | public function getLastRequest() 46 | { 47 | return $this->__getLastRequest(); 48 | } 49 | 50 | /** 51 | * Get the last response 52 | * 53 | * @return mixed 54 | */ 55 | public function getLastResponse() 56 | { 57 | return $this->__getLastResponse(); 58 | } 59 | 60 | /** 61 | * Get the last request headers 62 | * 63 | * @return mixed 64 | */ 65 | public function getLastRequestHeaders() 66 | { 67 | return $this->__getLastRequestHeaders(); 68 | } 69 | 70 | /** 71 | * Get the last response headers 72 | * 73 | * @return mixed 74 | */ 75 | public function getLastResponseHeaders() 76 | { 77 | return $this->__getLastResponseHeaders(); 78 | } 79 | 80 | /** 81 | * Get the types 82 | * 83 | * @return mixed 84 | */ 85 | public function getTypes() 86 | { 87 | return $this->__getTypes(); 88 | } 89 | 90 | /** 91 | * Get all the set cookies 92 | * 93 | * @return mixed 94 | */ 95 | public function getCookies() 96 | { 97 | return $this->__getCookies(); 98 | } 99 | 100 | /** 101 | * Set a new cookie 102 | * 103 | * @param string $name 104 | * @param string $value 105 | * 106 | * @return $this 107 | */ 108 | public function cookie($name, $value) 109 | { 110 | $this->__setCookie($name, $value); 111 | 112 | return $this; 113 | } 114 | 115 | /** 116 | * Set the location 117 | * 118 | * @param string $location 119 | * 120 | * @return $this 121 | */ 122 | public function location($location = '') 123 | { 124 | $this->__setLocation($location); 125 | 126 | return $this; 127 | } 128 | 129 | /** 130 | * Set the Soap headers 131 | * 132 | * @param array $headers 133 | * 134 | * @return $this 135 | */ 136 | protected function headers(array $headers = []) 137 | { 138 | $this->__setSoapHeaders($headers); 139 | 140 | return $this; 141 | } 142 | 143 | /** 144 | * Do soap request 145 | * 146 | * @param string $request 147 | * @param string $location 148 | * @param string $action 149 | * @param string $version 150 | * @param string $one_way 151 | * 152 | * @return mixed 153 | */ 154 | public function doRequest($request, $location, $action, $version, $one_way) 155 | { 156 | return $this->__doRequest($request, $location, $action, $version, $one_way); 157 | } 158 | 159 | /** 160 | * Do a soap call on the webservice client 161 | * 162 | * @param string $function 163 | * @param array $params 164 | * 165 | * @return mixed 166 | */ 167 | public function call($function, $params) 168 | { 169 | return call_user_func_array([$this, $function], $params); 170 | } 171 | 172 | /** 173 | * Allias to do a soap call on the webservice client 174 | * 175 | * @param string $function 176 | * @param array $params 177 | * @param array $options 178 | * @param null $inputHeader 179 | * @param null $outputHeaders 180 | * 181 | * @return mixed 182 | */ 183 | public function SoapCall($function, 184 | array $params, 185 | array $options = null, 186 | $inputHeader = null, 187 | &$outputHeaders = null 188 | ) { 189 | return $this->__soapCall($function, $params, $options, $inputHeader, $outputHeaders); 190 | } 191 | } 192 | -------------------------------------------------------------------------------- /src/Artisaninweb/SoapWrapper/Exceptions/ServiceAlreadyExists.php: -------------------------------------------------------------------------------- 1 | wsdl = null; 56 | $this->client = null; 57 | $this->certificate = false; 58 | $this->options = []; 59 | $this->classmap = []; 60 | $this->headers = []; 61 | } 62 | 63 | /** 64 | * Set a custom client 65 | * 66 | * @param SoapClient $client 67 | * 68 | * @return $this 69 | */ 70 | public function client(SoapClient $client) 71 | { 72 | $this->client = $client; 73 | 74 | return $this; 75 | } 76 | 77 | /** 78 | * Get the custom client 79 | * 80 | * @return SoapClient 81 | */ 82 | public function getClient() 83 | { 84 | return $this->client; 85 | } 86 | 87 | /** 88 | * Set the wsdl of the service 89 | * 90 | * @param string $wsdl 91 | * 92 | * @return $this 93 | */ 94 | public function wsdl($wsdl) 95 | { 96 | $this->wsdl = $wsdl; 97 | 98 | return $this; 99 | } 100 | 101 | /** 102 | * Get the wsdl from the service 103 | * 104 | * @return string 105 | */ 106 | public function getWsdl() 107 | { 108 | return $this->wsdl; 109 | } 110 | 111 | /** 112 | * Set trace option - enables tracing of request 113 | * 114 | * @param boolean $trace 115 | * 116 | * @return $this 117 | */ 118 | public function trace($trace) 119 | { 120 | $this->trace = $trace; 121 | 122 | return $this; 123 | } 124 | 125 | /** 126 | * Get the trace option 127 | * 128 | * @return boolean 129 | */ 130 | public function getTrace() 131 | { 132 | return $this->trace; 133 | } 134 | 135 | /** 136 | * Set the WSDL cache 137 | * 138 | * @param $cache 139 | * 140 | * @return $this 141 | */ 142 | public function cache($cache) 143 | { 144 | $this->cache = $cache; 145 | 146 | return $this; 147 | } 148 | 149 | /** 150 | * Get the WSDL cache 151 | * 152 | * @return string 153 | */ 154 | public function getCache() 155 | { 156 | return $this->cache; 157 | } 158 | 159 | /** 160 | * @param array $classmap 161 | * 162 | * @return $this 163 | */ 164 | public function classMap(array $classmap) 165 | { 166 | $this->classmap = $classmap; 167 | 168 | return $this; 169 | } 170 | 171 | /** 172 | * Get the classmap 173 | * 174 | * @return array 175 | */ 176 | public function getClassmap() 177 | { 178 | $classmap = $this->classmap; 179 | $classes = [] ; 180 | 181 | if (!empty($classmap)) { 182 | foreach ($classmap as $class) { 183 | // Can't use end because of strict mode :( 184 | $name = current(array_slice(explode('\\', $class), -1, 1, true)); 185 | 186 | $classes[$name] = $class; 187 | } 188 | } 189 | 190 | return $classes; 191 | } 192 | 193 | /** 194 | * Set the extra options on the SoapClient 195 | * 196 | * @param array $options 197 | * 198 | * @return $this 199 | */ 200 | public function options(array $options) 201 | { 202 | $this->options = $options; 203 | 204 | return $this; 205 | } 206 | 207 | /** 208 | * Get the extra options 209 | * 210 | * @return array 211 | */ 212 | public function getOptions() 213 | { 214 | $options = [ 215 | 'trace' => $this->getTrace(), 216 | 'cache_wsdl' => $this->getCache(), 217 | 'classmap' => $this->getClassmap(), 218 | ]; 219 | 220 | if ($this->certificate) { 221 | $options['local_cert'] = $this->certificate; 222 | } 223 | 224 | $this->options = array_merge($options, $this->options); 225 | 226 | return $this->options; 227 | } 228 | 229 | /** 230 | * Set the certificate location 231 | * 232 | * @param string $certificate 233 | * 234 | * @return $this 235 | */ 236 | public function certificate($certificate) 237 | { 238 | if ($certificate) { 239 | $this->certificate = $certificate; 240 | } 241 | 242 | return $this; 243 | } 244 | 245 | /** 246 | * Get the headers 247 | * 248 | * @return array 249 | */ 250 | public function getHeaders() 251 | { 252 | return $this->headers; 253 | } 254 | 255 | /** 256 | * Create a new SoapHeader 257 | * 258 | * @param string $namespace 259 | * @param string $name 260 | * @param null $data 261 | * @param bool $mustUnderstand 262 | * @param null $actor 263 | * 264 | * @return $this 265 | */ 266 | public function header($namespace, $name, $data = null, $mustUnderstand = false, $actor = null) 267 | { 268 | if ($actor) { 269 | $this->headers[] = new SoapHeader($namespace, $name, $data, $mustUnderstand, $actor); 270 | } else { 271 | $this->headers[] = new SoapHeader($namespace, $name, $data, $mustUnderstand); 272 | } 273 | 274 | return $this; 275 | } 276 | 277 | /** 278 | * Set the Soap headers 279 | * 280 | * @param SoapHeader $header 281 | * 282 | * @return $this 283 | */ 284 | public function customHeader($header) 285 | { 286 | $this->headers[] = $header; 287 | 288 | return $this; 289 | } 290 | } 291 | -------------------------------------------------------------------------------- /src/Artisaninweb/SoapWrapper/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | app['config']['soapwrapper'])) { 29 | $soapWrapper->addByArray($this->app['config']['soapwrapper']); 30 | } 31 | 32 | $this->app->bindIf(SoapWrapper::class, function () use ($soapWrapper) { 33 | return $soapWrapper; 34 | }); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Artisaninweb/SoapWrapper/SoapWrapper.php: -------------------------------------------------------------------------------- 1 | services = []; 24 | } 25 | 26 | /** 27 | * Add a new service to the wrapper 28 | * 29 | * @param string $name 30 | * @param Closure $closure 31 | * 32 | * @return $this 33 | * @throws ServiceAlreadyExists 34 | */ 35 | public function add($name, Closure $closure) 36 | { 37 | if (!$this->has($name)) { 38 | $service = new Service(); 39 | 40 | $closure($service); 41 | 42 | $this->services[$name] = $service; 43 | 44 | return $this; 45 | } 46 | 47 | throw new ServiceAlreadyExists("Service '" . $name . "' already exists."); 48 | } 49 | 50 | /** 51 | * Add services by array 52 | * 53 | * @param array $services 54 | * 55 | * @return $this 56 | * 57 | * @throws ServiceAlreadyExists 58 | * @throws ServiceMethodNotExists 59 | */ 60 | public function addByArray(array $services = []) 61 | { 62 | if (!empty($services)) { 63 | foreach ($services as $name => $methods) { 64 | if (!$this->has($name)) { 65 | $service = new Service(); 66 | 67 | foreach ($methods as $method => $value) { 68 | if (method_exists($service, $method)) { 69 | $service->{$method}($value); 70 | } else { 71 | throw new ServiceMethodNotExists(sprintf( 72 | "Method '%s' does not exists on the %s service.", 73 | $method, 74 | $name 75 | )); 76 | } 77 | } 78 | 79 | $this->services[$name] = $service; 80 | 81 | continue; 82 | } 83 | 84 | throw new ServiceAlreadyExists(sprintf( 85 | "Service '%s' already exists.", 86 | $name 87 | )); 88 | } 89 | } 90 | 91 | return $this; 92 | } 93 | 94 | /** 95 | * Get the client 96 | * 97 | * @param string $name 98 | * @param Closure $closure 99 | * 100 | * @return mixed 101 | * @throws ServiceNotFound 102 | */ 103 | public function client($name, Closure $closure = null) 104 | { 105 | if ($this->has($name)) { 106 | /** @var Service $service */ 107 | $service = $this->services[$name]; 108 | 109 | if (is_null($service->getClient())) { 110 | $client = new Client($service->getWsdl(), $service->getOptions(), $service->getHeaders()); 111 | 112 | $service->client($client); 113 | } else { 114 | $client = $service->getClient(); 115 | } 116 | 117 | return $closure($client); 118 | } 119 | 120 | throw new ServiceNotFound("Service '" . $name . "' not found."); 121 | } 122 | 123 | /** 124 | * A easy access call method 125 | * 126 | * @param string $call 127 | * @param array $data 128 | * 129 | * @return mixed 130 | */ 131 | public function call($call, $data = [], $options = []) 132 | { 133 | list($name, $function) = explode('.', $call, 2); 134 | 135 | return $this->client($name, function ($client) use ($function, $data, $options) { 136 | /** @var Client $client */ 137 | return $client->SoapCall($function, $data, $options); 138 | }); 139 | } 140 | 141 | /** 142 | * Check if wrapper has service 143 | * 144 | * @param string $name 145 | * 146 | * @return bool 147 | */ 148 | public function has($name) 149 | { 150 | return (array_key_exists($name, $this->services)); 151 | } 152 | } 153 | --------------------------------------------------------------------------------