src/FMT/Infrastructure/Service/Nebook/SoapApi/CustomSoapClient.php line 45

Open in your IDE?
  1. <?php
  2. /**
  3.  * Author: Vladimir Bykovsky
  4.  * Date: 11.11.2021
  5.  * Time: 15:10
  6.  */
  7. namespace FMT\Infrastructure\Service\Nebook\SoapApi;
  8. use SoapClient;
  9. use DomDocument;
  10. /**
  11.  * Class Client
  12.  * @package FMT\Infrastructure\Service\Nebook\SoapApi
  13.  *
  14.  */
  15. class CustomSoapClient extends SoapClient
  16. {
  17.     /** @var Options */
  18.     private $options;
  19.     /**
  20.      * __construct
  21.      * CustomSoapClient constructor.
  22.      * @param mixed $wsdl NULL for non-wsdl mode or URL string for wsdl mode
  23.      * @param array $options
  24.      */
  25.     public function __construct($wsdl, $options)
  26.     {
  27.         $this->options = $options;
  28.         parent::__construct($wsdl, $options);
  29.     }
  30.     /**
  31.      * __doRequest
  32.      * @param string $request
  33.      * @param string $location
  34.      * @param string $action
  35.      * @param int $version
  36.      * @param int $oneWay
  37.      * @return string
  38.      */
  39.     public function __doRequest($request, $location, $action, $version, $oneWay = 0)
  40.     {
  41.         $request = preg_replace('/SOAP-ENV/', 'soap', $request);
  42.         $request = preg_replace('/<ns1:/', '<', $request);
  43.         $request = preg_replace('/ns1:/', '', $request);
  44.         $request = str_replace(['/ns1:'], ['/'], $request);
  45.         $request = preg_replace('/ xmlns:ns1="(.*)">/', '>', $request, 1);
  46.         $xmlns = $this->options['xmlns'];
  47.         $xsi = 'http://www.w3.org/2001/XMLSchema-instance';
  48.         $xsd = 'http://www.w3.org/2001/XMLSchema';
  49.         $dom = new DomDocument('1.0', 'UTF-8');
  50.         $dom->preserveWhiteSpace = false;
  51.         $dom->loadXML($request);
  52.         $nodes = $dom->getElementsByTagName('Envelope');
  53.         foreach ($nodes as $node) {
  54.             $node->setAttribute('xmlns:xsi', $xsi);
  55.             $node->setAttribute('xmlns:xsd', $xsd);
  56.         }
  57.         $nodes = $dom->getElementsByTagName('Body');
  58.         foreach ($nodes as $node) {
  59.             $childNodes = $node->childNodes;
  60.             if (!$childNodes) continue;
  61.             foreach ($childNodes as $curNode) {
  62.                 $curNode->setAttribute('xmlns', $xmlns);
  63.             }
  64.         }
  65.         $nodes = $dom->getElementsByTagName('UserCredentials');
  66.         foreach ($nodes as $node) {
  67.             $node->setAttribute('xmlns', $xmlns);
  68.         }
  69.         $request = $dom->saveXML();
  70.         return parent::__doRequest($request, $location, $action, $version, $oneWay);
  71.     }
  72. }