Completed
Pull Request — master (#2)
by Mathieu
05:06 queued 01:16
created

Curl::getErrorMsg()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
namespace Suricate;
3
4
/**
5
 * Curl extension for Suricate
6
 *
7
 * @package Suricate
8
 * @author  Mathieu LESNIAK <[email protected]>
9
 *
10
 * @property string $userAgent
11
 * @property int $timeout
12
 * @property string $proxyHost
13
 * @property int $proxyPort
14
 * @property string $referer
15
 * @property string $cookie
16
 * @property string $userAgent
17
 * @property mixed $postFields
18
 * @property string $login
19
 * @property string $password
20
 * @property array $headers
21
 */
22
23
class Curl extends Service
24
{
25
    protected $parametersList   = [
26
        'userAgent',
27
        'timeout',
28
        'proxyHost',
29
        'proxyPort',
30
        'referer',
31
        'cookie',
32
        'userAgent',
33
        'postFields',
34
        'login',
35
        'password',
36
        'headers',
37
    ];
38
39
    private $request;
40
    private $response;
41
    private $responseData;
42
    private $errorMsg;
43
    private $errorCode;
44
45 1
    public function __construct()
46
    {
47 1
        parent::__construct();
48
49 1
        $this->request  = new Request();
50 1
        $this->response = new Request();
51 1
        $this->headers  = [];
52 1
    }
53
54 1
    public function setUrl($url)
55
    {
56 1
        $this->request->setUrl($url);
57
58 1
        return $this;
59
    }
60
61 1
    public function getUrl()
62
    {
63 1
        return $this->request->getUrl();
64
    }
65
66 1
    public function setMethod($method)
67
    {
68 1
        $this->request->setMethod($method);
69
70 1
        return $this;
71
    }
72
73 1
    public function setUserAgent($userAgent)
74
    {
75 1
        $this->userAgent = $userAgent;
76
77 1
        return $this;
78
    }
79
80 1
    public function addHeader($headerLine)
81
    {
82 1
        $headers = $this->headers;
83 1
        $headers[] = $headerLine;
84
85 1
        $this->headers = $headers;
86
87 1
        return $this;
88
    }
89
    
90
    public function send()
91
    {
92
        $curlHandler     = curl_init($this->request->getUrl());
93
94
        if ($curlHandler === false) {
95
            throw new \Exception('Can\'t init curl');
96
        }
97
        $curlOptions = $this->generateCurlOptions();
98
        curl_setopt_array($curlHandler, $curlOptions);
99
100
        $curlResponse       = curl_exec($curlHandler);
101
        if ($curlResponse === false) {
102
            $this->errorMsg     = curl_error($curlHandler);
103
            $this->errorCode    = curl_errno($curlHandler);
104
105
            return false;
106
        }
107
        // No return transfer option
108
        if (is_bool($curlResponse)) {
109
            return true;
110
        }
111
112
        $this->responseData = curl_getinfo($curlHandler);
113
        $this->response->setUrl($this->responseData['url']);
114
        $redirectCount      = curl_getinfo($curlHandler, CURLINFO_REDIRECT_COUNT);
115
116
        $splittedResponse   = explode("\r\n\r\n", $curlResponse, $redirectCount + 2);
117
        $lastHeader         = $splittedResponse[$redirectCount];
118
    
119
        // get headers out of response
120
        $headers = explode("\n", trim($lastHeader));
121
        array_shift($headers);
122
    
123
        foreach ($headers as $headerLine) {
124
            preg_match('|^([\d\w\s_-]*):(.*)|', $headerLine, $matches);
125
            if (isset($matches[1])) {
126
                $this->response->addHeader($matches[1], trim($matches[2]));
127
            }
128
        }
129
130
131
        // Reponse data
132
        $this->response->setHttpCode($this->responseData['http_code']);
133
        $this->response->setBody(substr($curlResponse, $this->responseData['header_size']));
134
135
        return $this;
136
    }
137
138
    private function generateCurlOptions()
139
    {
140
        $curlOptions = [
141
            CURLOPT_RETURNTRANSFER  => true,
142
            CURLOPT_HEADER          => true,
143
            CURLINFO_HEADER_OUT     => true,
144
            CURLOPT_FOLLOWLOCATION  => true,
145
            CURLOPT_SSL_VERIFYPEER  => false,
146
            CURLOPT_SSL_VERIFYHOST  => false
147
        ];
148
        
149
        $parametersMapping = [
150
            CURLOPT_CONNECTTIMEOUT  => 'timeout',
151
            CURLOPT_PROXY           => 'proxyHost',
152
            CURLOPT_PROXYPORT       => 'proxyPort',
153
            CURLOPT_REFERER         => 'referer',
154
            CURLOPT_COOKIE          => 'cookie',
155
            CURLOPT_USERAGENT       => 'userAgent',
156
            CURLOPT_HTTPHEADER      => 'headers',
157
        ];
158
159
        foreach ($parametersMapping as $curlKey => $optionKey) {
160
            if (($value = $this->getParameter($optionKey)) !== null) {
161
                $curlOptions[$curlKey] = $value;
162
            }
163
        }
164
165
        //
166
        // Method management
167
        //
168
        if ($this->request->getMethod() == Request::HTTP_METHOD_GET) {
169
            $curlOptions[CURLOPT_HTTPGET] = true;
170
        } elseif ($this->request->getMethod() == Request::HTTP_METHOD_POST) {
171
            $curlOptions[CURLOPT_POST] = true;
172
            if ($this->getParameter('postFields') !== null) {
173
                $curlOptions[CURLOPT_POSTFIELDS] = $this->getParameter('postFields');
174
            }
175
        } elseif ($this->request->getMethod() == Request::HTTP_METHOD_PUT) {
176
            $curlOptions[CURLOPT_PUT] = true;
177
        } elseif ($this->request->getMethod() == Request::HTTP_METHOD_HEAD) {
178
            $curlOptions[CURLOPT_NOBODY] = true;
179
        }
180
181
        return $curlOptions;
182
    }
183
184
    public function getResponse()
185
    {
186
        return $this->response;
187
    }
188
189
    public function getHeaders()
190
    {
191
        return $this->response->getHeaders();
192
    }
193
194
    public function getHttpCode()
195
    {
196
        return isset($this->responseData['http_code']) ? $this->responseData['http_code'] : null;
197
    }
198
199
    public function getErrorMsg()
200
    {
201
        return $this->errorMsg;
202
    }
203
204
    public function getErrorCode()
205
    {
206
        return $this->errorCode;
207
    }
208
}
209