class SoapController extends Controller
{
/** @var \Artisaninweb\SoapWrapper\SoapWrapper */
protected $soap;
/** @var \Artisaninweb\SoapWrapper\Client */
protected $client;
public function __construct(SoapWrapper $soap)
{
$this->soap = $soap;
$this->client = null;
}
public function getComputers()
{
try {
$clientId = request()->get('clientId', 0);
$orgId = request()->get('orgId', 0);
$this->initSoap();
$response = $this->client->call('GetComps', [
[
'ClientId' => $clientId,
'OrgId' => $orgId,
],
]);
$computers = json_decode($response->GetCompsResult, true);
return response()->json($computers, 200);
} catch (\Throwable $e) {
return response()->json([
'message' => $e->getMessage(),
], 500);
}
}
/**
* @throws \Exception
*/
protected function initSoap()
{
$clientName = 'some_name';
$wsdl = config('app.soap_wsdl');
if (!$wsdl) throw new \Exception('Invalid wsdl');
$version = $this->getSoapVersion();
$this->soap->add($clientName, function (SoapService $service) use ($wsdl, $version) {
$service
->wsdl($wsdl)
->options(['soap_version' => $version]);
});
$this->client = $this->soap->client($clientName, function ($client) { return $client; });
}
/**
* @return int
* @throws \Exception
*/
protected function getSoapVersion()
{
switch (config('app.soap_version')) {
case '1.1':
return SOAP_1_1;
case '1.2':
return SOAP_1_2;
default:
throw new \Exception('Invalid SOAP version');
}
}
}