1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* MIT License |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 MZ Desenvolvimento de Sistemas LTDA |
6
|
|
|
* |
7
|
|
|
* @author Francimar Alves <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
11
|
|
|
* in the Software without restriction, including without limitation the rights |
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
14
|
|
|
* furnished to do so, subject to the following conditions: |
15
|
|
|
* |
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
17
|
|
|
* copies or substantial portions of the Software. |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25
|
|
|
* SOFTWARE. |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
namespace NFe\Common; |
29
|
|
|
|
30
|
|
|
use Curl\Curl; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Faz requisições SOAP 1.2 |
34
|
|
|
*/ |
35
|
|
|
class CurlSoap extends Curl |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
const ENVELOPE = <<<XML |
39
|
|
|
<?xml version="1.0" encoding="utf-8"?> |
40
|
|
|
<soap12:Envelope |
41
|
|
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
42
|
|
|
xmlns:xsd="http://www.w3.org/2001/XMLSchema" |
43
|
|
|
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> |
44
|
|
|
<soap12:Body/> |
45
|
|
|
</soap12:Envelope> |
46
|
|
|
XML; |
47
|
|
|
|
48
|
|
|
private $certificate; |
49
|
|
|
private $private_key; |
50
|
|
|
private static $post_fn; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Construct |
54
|
|
|
* |
55
|
|
|
* @access public |
56
|
|
|
* @param $base_url |
57
|
|
|
* @throws \ErrorException |
58
|
|
|
*/ |
59
|
6 |
|
public function __construct($base_url = null) |
60
|
|
|
{ |
61
|
6 |
|
parent::__construct($base_url); |
62
|
6 |
|
$this->setHeader('Content-Type', 'application/soap+xml; charset=utf-8'); |
63
|
6 |
|
$this->setOpt(CURLOPT_SSL_VERIFYPEER, false); |
64
|
6 |
|
$this->setOpt(CURLOPT_SSLVERSION, 1); |
65
|
6 |
|
$this->setConnectTimeout(4); |
66
|
6 |
|
$this->setTimeout(6); |
67
|
6 |
|
$this->setXmlDecoder(function ($response) { |
68
|
|
|
$dom = new \DOMDocument(); |
69
|
|
|
$xml_obj = $dom->loadXML($response); |
70
|
|
|
if (!($xml_obj === false)) { |
71
|
|
|
$response = $dom; |
72
|
|
|
} |
73
|
|
|
return $response; |
74
|
6 |
|
}); |
75
|
6 |
|
} |
76
|
|
|
|
77
|
14 |
|
public static function setPostFunction($post_fn) |
78
|
|
|
{ |
79
|
14 |
|
return self::$post_fn = $post_fn; |
80
|
|
|
} |
81
|
|
|
|
82
|
4 |
|
public function setCertificate($certificate) |
83
|
|
|
{ |
84
|
4 |
|
$this->certificate = $certificate; |
85
|
4 |
|
} |
86
|
|
|
|
87
|
5 |
|
public function getCertificate() |
88
|
|
|
{ |
89
|
5 |
|
return $this->certificate; |
90
|
|
|
} |
91
|
|
|
|
92
|
4 |
|
public function setPrivateKey($private_key) |
93
|
|
|
{ |
94
|
4 |
|
$this->private_key = $private_key; |
95
|
4 |
|
} |
96
|
|
|
|
97
|
5 |
|
public function getPrivateKey() |
98
|
|
|
{ |
99
|
5 |
|
return $this->private_key; |
100
|
|
|
} |
101
|
|
|
|
102
|
5 |
|
public function send($url, $body) |
103
|
|
|
{ |
104
|
5 |
|
$this->setOpt(CURLOPT_SSLCERT, $this->getCertificate()); |
105
|
5 |
|
$this->setOpt(CURLOPT_SSLKEY, $this->getPrivateKey()); |
106
|
5 |
|
if ($body instanceof \DOMDocument) { |
107
|
5 |
|
$body = $body->saveXML($body->documentElement); |
108
|
|
|
} |
109
|
5 |
|
$dom = new \DOMDocument(); |
110
|
5 |
|
$dom->preserveWhiteSpace = false; |
111
|
5 |
|
$dom->loadXML(self::ENVELOPE); |
112
|
5 |
|
$envelope = $dom->saveXML(); |
113
|
5 |
|
$data = str_replace('<soap12:Body/>', '<soap12:Body>'.$body.'</soap12:Body>', $envelope); |
114
|
5 |
|
if (is_null(self::$post_fn)) { |
115
|
|
|
$this->post($url, $data); |
116
|
|
|
} else { |
117
|
5 |
|
call_user_func_array(self::$post_fn, [$this, $url, $data]); |
118
|
|
|
} |
119
|
5 |
|
if (!$this->error) { |
120
|
3 |
|
return $this->response; |
121
|
|
|
} |
122
|
2 |
|
if (!empty($this->rawResponse) && ($this->response instanceof \DOMDocument)) { |
123
|
|
|
$text = $this->response->getElementsByTagName('Text'); |
124
|
|
|
if ($text->length == 1) { |
125
|
|
|
throw new \Exception($text->item(0)->nodeValue, $this->errorCode); |
126
|
|
|
} |
127
|
|
|
} |
128
|
2 |
|
$transfer = $this->getInfo(CURLINFO_PRETRANSFER_TIME); |
129
|
2 |
|
if ($transfer == 0) { // never started the transfer |
130
|
2 |
|
throw new \NFe\Exception\NetworkException($this->errorMessage, $this->errorCode); |
131
|
|
|
} |
132
|
|
|
throw new \NFe\Exception\IncompleteRequestException($this->errorMessage, $this->errorCode); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|