|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NFePHP\Common\Soap; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* SoapClient based in cURL class |
|
7
|
|
|
* |
|
8
|
|
|
* @category NFePHP |
|
9
|
|
|
* @package NFePHP\Common\Soap\SoapCurl |
|
10
|
|
|
* @copyright NFePHP Copyright (c) 2016 |
|
11
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPLv3+ |
|
12
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
|
13
|
|
|
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+ |
|
14
|
|
|
* @author Roberto L. Machado <linux.rlm at gmail dot com> |
|
15
|
|
|
* @link http://github.com/nfephp-org/sped-common for the canonical source repository |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
use NFePHP\Common\Soap\SoapBase; |
|
19
|
|
|
use NFePHP\Common\Soap\SoapInterface; |
|
20
|
|
|
use NFePHP\Common\Soap\SoapData; |
|
21
|
|
|
use NFePHP\Common\Exception\SoapException; |
|
22
|
|
|
use NFePHP\Common\Certificate; |
|
23
|
|
|
use Psr\Log\LoggerInterface; |
|
24
|
|
|
|
|
25
|
|
|
class SoapCurl extends SoapBase implements SoapInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Constructor |
|
29
|
|
|
* @param Certificate $certificate |
|
30
|
|
|
* @param LoggerInterface $logger |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(Certificate $certificate = null, LoggerInterface $logger = null) |
|
33
|
|
|
{ |
|
34
|
|
|
parent::__construct($certificate, $logger); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Send soap message to url |
|
39
|
|
|
* @param string $url |
|
40
|
|
|
* @param string $operation |
|
41
|
|
|
* @param string $action |
|
42
|
|
|
* @param int $soapver |
|
43
|
|
|
* @param array $parameters |
|
44
|
|
|
* @param array $namespaces |
|
45
|
|
|
* @param string $request |
|
46
|
|
|
* @param \SoapHeader $soapheader |
|
47
|
|
|
* @return string |
|
48
|
|
|
* @throws \NFePHP\Common\Exception\SoapException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function send( |
|
51
|
|
|
$url, |
|
52
|
|
|
$operation = '', |
|
53
|
|
|
$action = '', |
|
54
|
|
|
$soapver = SOAP_1_2, |
|
55
|
|
|
$parameters = [], |
|
56
|
|
|
$namespaces = [], |
|
57
|
|
|
$request = '', |
|
58
|
|
|
$soapheader = null |
|
59
|
|
|
) { |
|
60
|
|
|
$data = new SoapData(); |
|
61
|
|
|
$data->urlService = $url; |
|
62
|
|
|
$data->urlAction = $action; |
|
63
|
|
|
$data->soapNamespaces = $namespaces; |
|
64
|
|
|
$data->envelopedData = $this->makeEnvelopeSoap($request, $namespaces, $soapver, $soapheader); |
|
65
|
|
|
$data->contentType = 'application/soap+xml'; |
|
66
|
|
|
|
|
67
|
|
|
return $this->send2($data); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function send2(SoapData $data): string |
|
71
|
|
|
{ |
|
72
|
|
|
$parameters = self::buildParameters($data); |
|
73
|
|
|
$this->requestHead = implode('\n', $parameters); |
|
74
|
|
|
$this->requestBody = $data->envelopedData; |
|
75
|
|
|
|
|
76
|
|
|
$url = $data->urlService; |
|
77
|
|
|
|
|
78
|
|
|
$httpcode = 0; |
|
79
|
|
|
try { |
|
80
|
|
|
$curl = $this->create_curl($url, $data->envelopedData, $parameters); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
$response = curl_exec($curl); |
|
83
|
|
|
|
|
84
|
|
|
$httpcode = $this->dispose_curl($curl, $response, $data->urlMethod); |
|
|
|
|
|
|
85
|
|
|
} catch (\Exception $e) { |
|
86
|
|
|
throw SoapException::unableToLoadCurl($e->getMessage()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ($this->soaperror != '') { |
|
90
|
|
|
throw SoapException::soapFault($this->soaperror . " [$url]"); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if ($httpcode != 200) { |
|
94
|
|
|
throw SoapException::soapFault(" [$url]" . $this->responseHead); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $this->responseBody; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
private static function buildParameters(SoapData $data): array |
|
101
|
|
|
{ |
|
102
|
|
|
$msgSize = strlen($data->envelopedData); |
|
103
|
|
|
$parameters = array( |
|
104
|
|
|
"Content-Type: $data->contentType;charset=\"utf-8\"", |
|
105
|
|
|
"Content-Length: $msgSize", |
|
106
|
|
|
); |
|
107
|
|
|
if (!empty($data->urlMethod)) { |
|
108
|
|
|
$parameters[0] .= ";action=\"$data->urlMethod\""; |
|
109
|
|
|
} |
|
110
|
|
|
if (!empty($data->urlAction)) { |
|
111
|
|
|
$parameters[] = "SOAPAction: $data->urlAction"; |
|
112
|
|
|
} |
|
113
|
|
|
return $parameters; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Set proxy into cURL parameters |
|
118
|
|
|
* @param resource $oCurl |
|
119
|
|
|
*/ |
|
120
|
|
|
private function setCurlProxy(&$oCurl) |
|
121
|
|
|
{ |
|
122
|
|
|
if ($this->proxyIP != '') { |
|
123
|
|
|
curl_setopt($oCurl, CURLOPT_HTTPPROXYTUNNEL, 1); |
|
124
|
|
|
curl_setopt($oCurl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); |
|
125
|
|
|
curl_setopt($oCurl, CURLOPT_PROXY, $this->proxyIP . ':' . $this->proxyPort); |
|
126
|
|
|
if ($this->proxyUser != '') { |
|
127
|
|
|
curl_setopt($oCurl, CURLOPT_PROXYUSERPWD, $this->proxyUser . ':' . $this->proxyPass); |
|
128
|
|
|
curl_setopt($oCurl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
private function createCurl($url, $envelope, $parameters) |
|
|
|
|
|
|
134
|
|
|
{ |
|
135
|
|
|
$oCurl = curl_init(); |
|
136
|
|
|
$this->setCurlProxy($oCurl); |
|
137
|
|
|
curl_setopt($oCurl, CURLOPT_URL, $url); |
|
138
|
|
|
curl_setopt($oCurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); |
|
139
|
|
|
curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $this->soaptimeout); |
|
140
|
|
|
curl_setopt($oCurl, CURLOPT_TIMEOUT, $this->soaptimeout + 30); |
|
141
|
|
|
curl_setopt($oCurl, CURLOPT_HEADER, 1); |
|
142
|
|
|
curl_setopt($oCurl, CURLOPT_VERBOSE, 1); |
|
143
|
|
|
// curl_setopt($oCurl, CURLOPT_FAILONERROR, 1); |
|
|
|
|
|
|
144
|
|
|
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 0); |
|
145
|
|
|
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, 0); |
|
146
|
|
|
|
|
147
|
|
|
if (!$this->disablesec) { |
|
148
|
|
|
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 2); |
|
149
|
|
|
if (is_file($this->casefaz)) { |
|
150
|
|
|
curl_setopt($oCurl, CURLOPT_CAINFO, $this->casefaz); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
curl_setopt($oCurl, CURLOPT_SSLVERSION, $this->soapprotocol); |
|
155
|
|
|
curl_setopt($oCurl, CURLOPT_SSLCERT, $this->tempdir . $this->certfile); |
|
156
|
|
|
curl_setopt($oCurl, CURLOPT_SSLKEY, $this->tempdir . $this->prifile); |
|
157
|
|
|
|
|
158
|
|
|
if (!empty($this->temppass)) { |
|
159
|
|
|
curl_setopt($oCurl, CURLOPT_KEYPASSWD, $this->temppass); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1); |
|
163
|
|
|
|
|
164
|
|
|
if (!empty($envelope)) { |
|
165
|
|
|
curl_setopt($oCurl, CURLOPT_POST, 1); |
|
166
|
|
|
curl_setopt($oCurl, CURLOPT_POSTFIELDS, $envelope); |
|
167
|
|
|
curl_setopt($oCurl, CURLOPT_HTTPHEADER, $parameters); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return $oCurl; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
private function disposeCurl($oCurl, $response, $operation = '') |
|
|
|
|
|
|
174
|
|
|
{ |
|
175
|
|
|
$this->soaperror = curl_error($oCurl); |
|
176
|
|
|
$ainfo = curl_getinfo($oCurl); |
|
177
|
|
|
|
|
178
|
|
|
if (is_array($ainfo)) { |
|
179
|
|
|
$this->soapinfo = $ainfo; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
$headsize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE); |
|
183
|
|
|
$httpcode = curl_getinfo($oCurl, CURLINFO_HTTP_CODE); |
|
184
|
|
|
curl_close($oCurl); |
|
185
|
|
|
|
|
186
|
|
|
if ($response) { |
|
187
|
|
|
$this->responseHead = trim(substr($response, 0, $headsize)); |
|
188
|
|
|
$this->responseBody = trim(substr($response, $headsize)); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$this->saveDebugFiles( |
|
192
|
|
|
$operation, |
|
193
|
|
|
$this->requestHead . "\n" . $this->requestBody, |
|
194
|
|
|
$this->responseHead . "\n" . $this->responseBody |
|
195
|
|
|
); |
|
196
|
|
|
|
|
197
|
|
|
return $httpcode; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.