1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NFePHP\eSocial\Common\Soap; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* SoapClient based in cURL class |
7
|
|
|
* |
8
|
|
|
* @category library |
9
|
|
|
* @package NFePHP\eSocial |
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\Certificate; |
19
|
|
|
use NFePHP\Common\Exception\SoapException; |
20
|
|
|
use Psr\Log\LoggerInterface; |
21
|
|
|
|
22
|
|
|
class SoapCurl extends SoapBase implements SoapInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Constructor |
26
|
|
|
* @param Certificate $certificate |
27
|
|
|
* @param LoggerInterface $logger |
28
|
|
|
*/ |
29
|
|
|
public function __construct(Certificate $certificate = null, LoggerInterface $logger = null) |
30
|
|
|
{ |
31
|
|
|
parent::__construct($certificate, $logger); |
32
|
|
|
} |
33
|
|
|
/** |
34
|
|
|
* Send soap message to url |
35
|
|
|
* |
36
|
|
|
* @param string $operation |
37
|
|
|
* @param string $url |
38
|
|
|
* @param string $action |
39
|
|
|
* @param string $envelope |
40
|
|
|
* @param array $parameters |
41
|
|
|
* @return string |
42
|
|
|
* @throws \NFePHP\Common\Exception\SoapException |
43
|
|
|
*/ |
44
|
|
|
public function send( |
45
|
|
|
$operation, |
46
|
|
|
$url, |
47
|
|
|
$action, |
48
|
|
|
$envelope, |
49
|
|
|
$parameters |
50
|
|
|
) { |
51
|
|
|
$response = ''; |
52
|
|
|
$this->requestHead = implode("\n", $parameters); |
53
|
|
|
$this->requestBody = $envelope; |
54
|
|
|
try { |
55
|
|
|
$this->saveTemporarilyKeyFiles(); |
56
|
|
|
$oCurl = curl_init(); |
57
|
|
|
$this->setCurlProxy($oCurl); |
58
|
|
|
curl_setopt($oCurl, CURLOPT_URL, $url); |
59
|
|
|
curl_setopt($oCurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); |
60
|
|
|
curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $this->soaptimeout); |
61
|
|
|
curl_setopt($oCurl, CURLOPT_TIMEOUT, $this->soaptimeout + 20); |
62
|
|
|
curl_setopt($oCurl, CURLOPT_HEADER, 1); |
63
|
|
|
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 0); |
64
|
|
|
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, 0); |
65
|
|
|
if (! $this->disablesec) { |
66
|
|
|
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 2); |
67
|
|
|
if (is_file($this->casefaz)) { |
68
|
|
|
curl_setopt($oCurl, CURLOPT_CAINFO, $this->casefaz); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
curl_setopt($oCurl, CURLOPT_SSLVERSION, $this->soapprotocol); |
72
|
|
|
curl_setopt($oCurl, CURLOPT_SSLCERT, $this->tempdir.$this->certfile); |
73
|
|
|
curl_setopt($oCurl, CURLOPT_SSLKEY, $this->tempdir.$this->prifile); |
74
|
|
|
if (! empty($this->temppass)) { |
75
|
|
|
curl_setopt($oCurl, CURLOPT_KEYPASSWD, $this->temppass); |
76
|
|
|
} |
77
|
|
|
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true); |
78
|
|
|
if (! empty($envelope)) { |
79
|
|
|
curl_setopt($oCurl, CURLOPT_POST, true); |
80
|
|
|
curl_setopt($oCurl, CURLOPT_POSTFIELDS, $envelope); |
81
|
|
|
curl_setopt($oCurl, CURLOPT_HTTPHEADER, $parameters); |
82
|
|
|
} |
83
|
|
|
$response = curl_exec($oCurl); |
84
|
|
|
$this->soaperror = curl_error($oCurl); |
85
|
|
|
$this->soaperror_code = curl_errno($oCurl); |
|
|
|
|
86
|
|
|
$ainfo = curl_getinfo($oCurl); |
87
|
|
|
if (is_array($ainfo)) { |
88
|
|
|
$this->soapinfo = $ainfo; |
89
|
|
|
} |
90
|
|
|
$headsize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE); |
91
|
|
|
$httpcode = curl_getinfo($oCurl, CURLINFO_HTTP_CODE); |
92
|
|
|
curl_close($oCurl); |
93
|
|
|
$this->responseHead = trim(substr($response, 0, $headsize)); |
94
|
|
|
$this->responseBody = trim(substr($response, $headsize)); |
95
|
|
|
$this->saveDebugFiles( |
96
|
|
|
$operation, |
97
|
|
|
$this->requestHead."\n".$this->requestBody, |
98
|
|
|
$this->responseHead."\n".$this->responseBody |
99
|
|
|
); |
100
|
|
|
} catch (\Exception $e) { |
101
|
|
|
throw SoapException::unableToLoadCurl($e->getMessage()); |
102
|
|
|
} |
103
|
|
|
if ($this->soaperror != '') { |
104
|
|
|
if (intval($this->soaperror_code) == 0) { |
|
|
|
|
105
|
|
|
$this->soaperror_code = 7; |
|
|
|
|
106
|
|
|
} |
107
|
|
|
throw SoapException::soapFault($this->soaperror." [$url]", $this->soaperror_code); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
if ($httpcode != 200) { |
110
|
|
|
if (intval($httpcode) == 0) { |
111
|
|
|
$httpcode = 52; |
112
|
|
|
} |
113
|
|
|
throw SoapException::soapFault(" [$url]" . $this->responseHead, $httpcode); |
114
|
|
|
} |
115
|
|
|
return $this->responseBody; |
116
|
|
|
} |
117
|
|
|
/** |
118
|
|
|
* Set proxy into cURL parameters |
119
|
|
|
* @param resource $oCurl |
120
|
|
|
*/ |
121
|
|
|
private function setCurlProxy(&$oCurl) |
122
|
|
|
{ |
123
|
|
|
if ($this->proxyIP != '') { |
124
|
|
|
curl_setopt($oCurl, CURLOPT_HTTPPROXYTUNNEL, 1); |
125
|
|
|
curl_setopt($oCurl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); |
126
|
|
|
curl_setopt($oCurl, CURLOPT_PROXY, $this->proxyIP.':'.$this->proxyPort); |
127
|
|
|
if ($this->proxyUser != '') { |
128
|
|
|
curl_setopt($oCurl, CURLOPT_PROXYUSERPWD, $this->proxyUser.':'.$this->proxyPass); |
129
|
|
|
curl_setopt($oCurl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
/** |
134
|
|
|
* Extract faultstring form response if exists |
135
|
|
|
* @param string $body |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
private function getFaultString($body) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
if (empty($body)) { |
141
|
|
|
return ''; |
142
|
|
|
} |
143
|
|
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
144
|
|
|
$dom->formatOutput = false; |
145
|
|
|
$dom->preserveWhiteSpace = false; |
146
|
|
|
$dom->loadXML($body); |
147
|
|
|
$faultstring = ''; |
148
|
|
|
$nodefault = ! empty($dom->getElementsByTagName('faultstring')->item(0)) |
149
|
|
|
? $dom->getElementsByTagName('faultstring')->item(0) |
150
|
|
|
: ''; |
151
|
|
|
if (!empty($nodefault)) { |
152
|
|
|
$faultstring = $nodefault->nodeValue; |
153
|
|
|
} |
154
|
|
|
return htmlentities($faultstring, ENT_QUOTES, 'UTF-8'); |
155
|
|
|
} |
156
|
|
|
/** |
157
|
|
|
* Recover WSDL form given URL |
158
|
|
|
* @param string $url |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
public function wsdl($url) |
162
|
|
|
{ |
163
|
|
|
$response = ''; |
|
|
|
|
164
|
|
|
$this->saveTemporarilyKeyFiles(); |
165
|
|
|
$url .= '?WSDL'; |
166
|
|
|
$oCurl = curl_init(); |
167
|
|
|
curl_setopt($oCurl, CURLOPT_URL, $url); |
168
|
|
|
curl_setopt($oCurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); |
169
|
|
|
curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $this->soaptimeout); |
170
|
|
|
curl_setopt($oCurl, CURLOPT_TIMEOUT, $this->soaptimeout + 20); |
171
|
|
|
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 0); |
172
|
|
|
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, 0); |
173
|
|
|
curl_setopt($oCurl, CURLOPT_SSLVERSION, $this->soapprotocol); |
174
|
|
|
curl_setopt($oCurl, CURLOPT_SSLCERT, $this->tempdir.$this->certfile); |
175
|
|
|
curl_setopt($oCurl, CURLOPT_SSLKEY, $this->tempdir.$this->prifile); |
176
|
|
|
if (! empty($this->temppass)) { |
177
|
|
|
curl_setopt($oCurl, CURLOPT_KEYPASSWD, $this->temppass); |
178
|
|
|
} |
179
|
|
|
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true); |
180
|
|
|
$response = curl_exec($oCurl); |
181
|
|
|
$soaperror = curl_error($oCurl); |
|
|
|
|
182
|
|
|
$ainfo = curl_getinfo($oCurl); |
|
|
|
|
183
|
|
|
$headsize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE); |
|
|
|
|
184
|
|
|
$httpcode = curl_getinfo($oCurl, CURLINFO_HTTP_CODE); |
185
|
|
|
curl_close($oCurl); |
186
|
|
|
if ($httpcode != 200) { |
187
|
|
|
return ''; |
188
|
|
|
} |
189
|
|
|
return $response; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.