|
1
|
|
|
<?php |
|
2
|
|
|
namespace Eduardokum\CorreiosPhp\Soap; |
|
3
|
|
|
|
|
4
|
|
|
use Eduardokum\CorreiosPhp\Contracts\Soap\Soap as SoapContract; |
|
5
|
|
|
use Eduardokum\CorreiosPhp\Exception\SoapException; |
|
6
|
|
|
|
|
7
|
|
|
abstract class Soap implements SoapContract |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var int |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $soapProtocol = self::SSL_DEFAULT; |
|
13
|
|
|
/** |
|
14
|
|
|
* @var int |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $soapTimeout = 20; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $proxyIP; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var int |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $proxyPort; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $proxyUser; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $proxyPass; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var bool |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $debugMode = false; |
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
public $response; |
|
41
|
|
|
/** |
|
42
|
|
|
* @var mixed |
|
43
|
|
|
*/ |
|
44
|
|
|
public $request; |
|
45
|
|
|
/** |
|
46
|
|
|
* @var mixed |
|
47
|
|
|
*/ |
|
48
|
|
|
public $soapError; |
|
49
|
|
|
/** |
|
50
|
|
|
* @var mixed |
|
51
|
|
|
*/ |
|
52
|
|
|
public $soapInfo; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set debug mode, this mode will save soap envelopes in temporary directory |
|
56
|
|
|
* @param bool $value |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
|
|
public function setDebugMode($value = false) |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->debugMode = $value; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Set timeout for communication |
|
66
|
|
|
* |
|
67
|
|
|
* @param int $timesecs |
|
68
|
|
|
* |
|
69
|
|
|
* @return int |
|
70
|
|
|
*/ |
|
71
|
|
|
public function timeout($timesecs) |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->soapTimeout = $timesecs; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Set security protocol |
|
78
|
|
|
* |
|
79
|
|
|
* @param int $protocol |
|
80
|
|
|
* |
|
81
|
|
|
* @return int |
|
82
|
|
|
*/ |
|
83
|
|
|
public function protocol($protocol = self::SSL_DEFAULT) |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->soapProtocol = $protocol; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Set proxy parameters |
|
90
|
|
|
* @param string $ip |
|
91
|
|
|
* @param int $port |
|
92
|
|
|
* @param string $user |
|
93
|
|
|
* @param string $password |
|
94
|
|
|
*/ |
|
95
|
|
|
public function proxy($ip, $port, $user, $password) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->proxyIP = $ip; |
|
98
|
|
|
$this->proxyPort = $port; |
|
99
|
|
|
$this->proxyUser = $user; |
|
100
|
|
|
$this->proxyPass = $password; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $url |
|
105
|
|
|
* @param array $action |
|
106
|
|
|
* @param string $request |
|
107
|
|
|
* @param array $namespaces |
|
108
|
|
|
* @param array $auth |
|
109
|
|
|
* |
|
110
|
|
|
* @return mixed |
|
111
|
|
|
*/ |
|
112
|
|
|
abstract public function send($url, array $action = [], $request = '', $namespaces = [], $auth = []); |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Mount soap envelope |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $request |
|
118
|
|
|
* @param array $namespaces |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function envelop($request, $namespaces = []) |
|
123
|
|
|
{ |
|
124
|
|
|
$namespaces = array_merge([ |
|
125
|
|
|
'xmlns:soap' => 'http://schemas.xmlsoap.org/soap/envelope/', |
|
126
|
|
|
'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", |
|
127
|
|
|
'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema", |
|
128
|
|
|
], $namespaces); |
|
129
|
|
|
|
|
130
|
|
|
$envelope = '<?xml version="1.0" encoding="UTF-8"?><soap:Envelope'; |
|
131
|
|
|
foreach ($namespaces as $key => $value) { |
|
132
|
|
|
$envelope .= vsprintf(' %s="%s"', [$key, $value]); |
|
133
|
|
|
} |
|
134
|
|
|
$envelope .= ">"; |
|
135
|
|
|
$envelope .= "<soap:Body>$request</soap:Body>"; |
|
136
|
|
|
$envelope .= "</soap:Envelope>"; |
|
137
|
|
|
return $envelope; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param $response |
|
142
|
|
|
* |
|
143
|
|
|
* @return \stdClass |
|
144
|
|
|
* @throws SoapException |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function response($response) |
|
147
|
|
|
{ |
|
148
|
|
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
|
149
|
|
|
$dom->preserveWhiteSpace = false; |
|
150
|
|
|
$dom->formatOutput = false; |
|
151
|
|
|
$dom->loadXML($response); |
|
152
|
|
|
|
|
153
|
|
|
if (strpos($response, 'faultstring') !== false) { |
|
154
|
|
|
$exception = $dom->getElementsByTagName('faultstring')->item(0)->nodeValue; |
|
155
|
|
|
throw new SoapException($exception); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$response = $dom->getElementsByTagName('Body') |
|
159
|
|
|
->item(0) // Get Body |
|
160
|
|
|
->childNodes->item(0); // Get Result Object; |
|
161
|
|
|
$response = simplexml_load_string($dom->saveXML($response), \SimpleXMLElement::class, LIBXML_NOCDATA); |
|
162
|
|
|
$response = json_encode($response, JSON_PRETTY_PRINT); |
|
163
|
|
|
return json_decode($response); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|