custom/plugins/UandiEfbErpSynchronization/src/Service/Api/SoapClient.php line 62

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Uandi\UandiEfbErpSynchronization\Service\Api;
  3. use Exception;
  4. use SoapFault;
  5. class SoapClient extends \SoapClient {
  6.     public ?string $username null;
  7.     public ?string $password null;
  8.     /**
  9.      *
  10.      * @param string $wsdl
  11.      * @param array|null $options
  12.      * @throws SoapFault
  13.      */
  14.     public function __construct($wsdl, ?array $options null) {
  15.         $wrappers stream_get_wrappers();
  16.         stream_wrapper_unregister('http');
  17.         stream_wrapper_register('http''Uandi\UandiEfbErpSynchronization\Service\Api\StreamWrapperHttpAuth');
  18.         if (in_array("https"$wrappers)) {
  19.             stream_wrapper_unregister('https');
  20.             stream_wrapper_register('https''Uandi\UandiEfbErpSynchronization\Service\Api\StreamWrapperHttpAuth');
  21.         }
  22.         if ($options) {
  23.             $this->username $options['user'];
  24.             StreamWrapperHttpAuth::$username $this->username;
  25.             $this->password $options['password'];
  26.             StreamWrapperHttpAuth::$password $this->password;
  27.         }
  28.         parent::__construct($wsdl, ($options ?: array()));
  29.         stream_wrapper_restore('http');
  30.         if (in_array("https"$wrappers)) stream_wrapper_restore('https');
  31.     }
  32.     /**
  33.      * @param string $request
  34.      * @param string $location
  35.      * @param string $action
  36.      * @param int $version
  37.      * @param bool $oneWay
  38.      * @return string|null
  39.      * @throws Exception
  40.      */
  41.     public function __doRequest(string $requeststring $locationstring $actionint $versionbool $oneWay false): ?string
  42.     {
  43.         $headers = array(
  44.             'User-Agent: PHP-SOAP',
  45.             'Content-Type: text/xml; charset=utf-8',
  46.             'SOAPAction: "' $action '"',
  47.             'Expect: 100-continue',
  48.             'Connection: Keep-Alive'
  49.         );
  50.         $this->__last_request_headers $headers;
  51.         $location $this->sanitizeUrl($location);
  52.         $ch curl_init($location);
  53.         curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
  54.         curl_setopt($chCURLOPT_POSTTRUE);
  55.         curl_setopt($chCURLOPT_POSTFIELDS$request);
  56.         curl_setopt($chCURLOPT_HTTP_VERSIONCURL_HTTP_VERSION_1_1);
  57.         curl_setopt($chCURLOPT_FAILONERRORFALSE);
  58.         curl_setopt($chCURLOPT_HTTPAUTHCURLAUTH_ANY);
  59.         curl_setopt($chCURLOPT_USERPWD$this->username ':' $this->password);
  60.         curl_setopt($chCURLOPT_HTTPAUTHCURLAUTH_NTLM);
  61.         curl_setopt($chCURLOPT_SSL_VERIFYPEERFALSE);
  62.         curl_setopt($chCURLOPT_SSL_VERIFYHOST2);
  63.         curl_setopt($chCURLOPT_HTTPHEADER$headers);
  64.         $response curl_exec($ch);
  65.         if (($info curl_getinfo($ch)) && $info['http_code'] == 200) {
  66.             return $response;
  67.         }
  68.         else {
  69.             if ($info['http_code'] == 401) {
  70.                 throw new Exception ('Access Denied'401);
  71.             }
  72.             else {
  73.                 if (curl_errno($ch) != 0) {
  74.                     throw new Exception(curl_error($ch), curl_errno($ch));
  75.                 }
  76.                 else {
  77.                     throw new Exception($response$info['http_code']);
  78.                 }
  79.             }
  80.         }
  81.     }
  82.     /**
  83.      * Sanitize URL to request to Sharepoint
  84.      *
  85.      * @param string $url
  86.      * @return string
  87.      */
  88.     protected function sanitizeUrl(string $url): string
  89.     {
  90.         return str_replace(" ""%20"$url);
  91.     }
  92. }