setName('soap:client') ->setDescription('Call a SOAP Client Service') ->setHelp('Call a SOAP Client service from wsdl') ->addOption('wsdl', null, InputOption::VALUE_REQUIRED, 'Soap WSDL') ->addOption('service', null, InputOption::VALUE_OPTIONAL, 'Webservice name') ->addOption('filename', null, InputOption::VALUE_OPTIONAL, 'File source of data in json format') ; } /** * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) { $wsdl = $input->getOption('wsdl'); if (is_null($wsdl)) { $output->writeln("Option --wdsl are required"); return; } try { $soap = new \SoapClient($wsdl); $service = $this->getService($input, $output, $soap); $data = $this->getFileData($input, $output); $result = $soap->$service($data); $output->writeln(PHP_EOL . "Result: {$wsdl} -> {$service}"); $output->writeln($result); } catch (\Exception $ex) { $output->writeln(sprintf('%s', $ex->getMessage())); } } /** * @param InputInterface $input * @param OutputInterface $output * @param \SoapClient $soap * * @return string */ protected function getService(InputInterface $input, OutputInterface $output, \SoapClient $soap) { $service = $input->getOption('service'); $methods = array_map(function ($method) { return explode('(', explode(' ', $method)[1])[0]; }, $soap->__getFunctions()); while (is_null($service) || !in_array($service, $methods)) { $output->writeln(PHP_EOL . 'Enter one service of the next list:' . PHP_EOL); $output->writeln(implode(', ', $methods)); $service = trim(fgets(STDIN)); if (!in_array($service, $methods)) { $output->writeln('Invalid Option'); } } return $service; } /** * @param InputInterface $input * @param OutputInterface $output * * @return array */ protected function getFileData(InputInterface $input, OutputInterface $output) { $filename = $input->getOption('filename'); while (is_null($filename) || !file_exists($filename)) { $output->writeln(PHP_EOL . 'Enter the source data filename. Press Enter to finish' . PHP_EOL); $stdin = fgets(STDIN); if ($stdin === PHP_EOL) { return; } $filename = trim($stdin); if (!file_exists($filename)) { $output->writeln('File not exists or not found!'); } } $data = json_decode(file_get_contents($filename), true); $output->writeln(PHP_EOL . 'Data Recived:'); $output->writeln(json_encode($data)); return $data; } }