Completed
Pull Request — master (#122)
by Roberto
02:26
created

SoapCurl   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 125
ccs 0
cts 97
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
F send() 0 82 9
A setCurlProxy() 0 12 3
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\Exception\SoapException;
21
use NFePHP\Common\Certificate;
22
use Psr\Log\LoggerInterface;
23
24
class SoapCurl extends SoapBase implements SoapInterface
25
{
26
    /**
27
     * Constructor
28
     * @param Certificate $certificate
29
     * @param LoggerInterface $logger
30
     */
31
    public function __construct(Certificate $certificate = null, LoggerInterface $logger = null)
32
    {
33
        parent::__construct($certificate, $logger);
34
    }
35
    
36
    /**
37
     * Send soap message to url
38
     * @param string $url
39
     * @param string $operation
40
     * @param string $action
41
     * @param int $soapver
42
     * @param array $parameters
43
     * @param array $namespaces
44
     * @param string $request
45
     * @param \SOAPHeader $soapheader
46
     * @return string
47
     * @throws \NFePHP\Common\Exception\SoapException
48
     */
49
    public function send(
50
        $url,
51
        $operation = '',
52
        $action = '',
53
        $soapver = SOAP_1_2,
54
        $parameters = [],
55
        $namespaces = [],
56
        $request = '',
57
        $soapheader = null
58
    ) {
59
        $response = '';
60
        $envelope = $this->makeEnvelopeSoap(
61
            $request,
62
            $namespaces,
63
            $soapver,
64
            $soapheader
65
        );
66
        $msgSize = strlen($envelope);
67
        $parameters = [
68
            "Content-Type: application/soap+xml;charset=utf-8;",
69
            "Content-length: $msgSize"
70
        ];
71
        if (!empty($action)) {
72
            $parameters[0] .= "action=$action";
73
        }
74
        $this->requestHead = implode("\n", $parameters);
75
        $this->requestBody = $envelope;
76
        
77
        try {
78
            $this->saveTemporarilyKeyFiles();
79
            $oCurl = curl_init();
80
            $this->setCurlProxy($oCurl);
81
            curl_setopt($oCurl, CURLOPT_URL, $url);
82
            curl_setopt($oCurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
83
            curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $this->soaptimeout);
84
            curl_setopt($oCurl, CURLOPT_TIMEOUT, $this->soaptimeout + 20);
85
            curl_setopt($oCurl, CURLOPT_HEADER, 1);
86
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 0);
87
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, 0);
88
            if (!$this->disablesec) {
89
                curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 2);
90
                if (is_file($this->casefaz)) {
91
                    curl_setopt($oCurl, CURLOPT_CAINFO, $this->casefaz);
92
                }
93
            }
94
            curl_setopt($oCurl, CURLOPT_SSLVERSION, $this->soapprotocol);
95
            curl_setopt($oCurl, CURLOPT_SSLCERT, $this->tempdir . $this->certfile);
96
            curl_setopt($oCurl, CURLOPT_SSLKEY, $this->tempdir . $this->prifile);
97
            curl_setopt($oCurl, CURLOPT_KEYPASSWD, $this->temppass);
98
            curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
99
            if (! empty($envelope)) {
100
                curl_setopt($oCurl, CURLOPT_POST, 1);
101
                curl_setopt($oCurl, CURLOPT_POSTFIELDS, $envelope);
102
                curl_setopt($oCurl, CURLOPT_HTTPHEADER, $parameters);
103
            }
104
            $response = curl_exec($oCurl);
105
            $this->soaperror = curl_error($oCurl);
106
            $ainfo = curl_getinfo($oCurl);
107
            if (is_array($ainfo)) {
108
                $this->soapinfo = $ainfo;
109
            }
110
            $headsize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE);
111
            $httpcode = curl_getinfo($oCurl, CURLINFO_HTTP_CODE);
112
            curl_close($oCurl);
113
            $this->responseHead = trim(substr($response, 0, $headsize));
114
            $this->responseBody = trim(substr($response, $headsize));
115
            $this->saveDebugFiles(
116
                $operation,
117
                $this->requestHead . "\n" . $this->requestBody,
118
                $this->responseHead . "\n" . $this->responseBody
119
            );
120
        } catch (\Exception $e) {
121
            throw SoapException::unableToLoadCurl($e->getMessage());
122
        }
123
        if ($this->soaperror != '') {
124
            throw SoapException::soapFault($this->soaperror);
125
        }
126
        if ($httpcode != 200) {
127
            throw SoapException::soapFault($this->responseHead);
128
        }
129
        return $this->responseBody;
130
    }
131
    
132
    /**
133
     * Set proxy into cURL parameters
134
     * @param resource $oCurl
135
     */
136
    private function setCurlProxy(&$oCurl)
137
    {
138
        if ($this->proxyIP != '') {
139
            curl_setopt($oCurl, CURLOPT_HTTPPROXYTUNNEL, 1);
140
            curl_setopt($oCurl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
141
            curl_setopt($oCurl, CURLOPT_PROXY, $this->proxyIP.':'.$this->proxyPort);
142
            if ($this->proxyUser != '') {
143
                curl_setopt($oCurl, CURLOPT_PROXYUSERPWD, $this->proxyUser.':'.$this->proxyPass);
144
                curl_setopt($oCurl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
145
            }
146
        }
147
    }
148
}
149