1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* php-guard/curl <https://github.com/php-guard/curl> |
4
|
|
|
* Copyright (C) ${YEAR} by Alexandre Le Borgne <[email protected]>. |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace PhpGuard\Curl; |
21
|
|
|
|
22
|
|
|
use PhpGuard\Curl\Collection\CurlOptions; |
23
|
|
|
use PhpGuard\Curl\Collection\Headers; |
24
|
|
|
|
25
|
|
|
class CurlRequestFactory |
26
|
|
|
{ |
27
|
|
|
const DEFAULT_CURL_OPTIONS = [ |
28
|
|
|
CURLOPT_RETURNTRANSFER => true, |
29
|
|
|
CURLOPT_HEADER => true, |
30
|
|
|
CURLINFO_HEADER_OUT => true, |
31
|
|
|
CURLOPT_ENCODING => '', |
32
|
|
|
CURLOPT_MAXREDIRS => 10, |
33
|
|
|
CURLOPT_TIMEOUT => 30, |
34
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
protected $defaultCurlOptions; |
38
|
|
|
protected $defaultHeaders; |
39
|
|
|
/** |
40
|
|
|
* @var null|string |
41
|
|
|
*/ |
42
|
|
|
private $baseUrl; |
43
|
|
|
/** |
44
|
|
|
* @var Curl |
45
|
|
|
*/ |
46
|
|
|
private $curl; |
47
|
|
|
|
48
|
|
|
public function __construct(Curl $curl, ?string $host = null) |
49
|
|
|
{ |
50
|
|
|
if ($host) { |
51
|
|
|
$host = rtrim($host, '/'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->defaultHeaders = new Headers(); |
55
|
|
|
$this->defaultCurlOptions = new CurlOptions(self::DEFAULT_CURL_OPTIONS); |
56
|
|
|
$this->baseUrl = $host; |
57
|
|
|
$this->curl = $curl; |
58
|
|
|
} |
59
|
|
|
|
60
|
45 |
|
public function create(string $method, string $url, $data = null, $query = null, array $headers = []): CurlRequest |
61
|
|
|
{ |
62
|
45 |
|
if ($this->baseUrl && is_null(parse_url($url, PHP_URL_HOST))) { |
|
|
|
|
63
|
3 |
|
if (isset($url[0]) && '/' != $url[0]) { |
64
|
3 |
|
$url = '/'.$url; |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
$url = $this->baseUrl.$url; |
68
|
|
|
} |
69
|
|
|
|
70
|
45 |
|
if (!empty($query)) { |
71
|
9 |
|
$url .= '?'.(is_string($query) ? $query : http_build_query($query, '', '&')); |
72
|
|
|
} |
73
|
|
|
|
74
|
45 |
|
$headers = $this->defaultHeaders->replace($headers)->all(); |
75
|
|
|
|
76
|
45 |
|
return new CurlRequest($this->curl, $url, $method, $data, $headers, $this->defaultCurlOptions->all()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* If set to false, ignore error "SSL certificate problem: unable to get local issuer certificate" |
81
|
|
|
* Default to true. |
82
|
|
|
* |
83
|
|
|
* @param bool $value |
84
|
|
|
* |
85
|
|
|
* @return CurlRequestFactory |
86
|
|
|
*/ |
87
|
|
|
public function setSslVerifyPeer(bool $value): self |
88
|
|
|
{ |
89
|
|
|
$this->defaultCurlOptions[CURLOPT_SSL_VERIFYPEER] = $value; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getDefaultCurlOptions(): CurlOptions |
95
|
|
|
{ |
96
|
|
|
return $this->defaultCurlOptions; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return Headers |
101
|
|
|
*/ |
102
|
|
|
public function getDefaultHeaders(): Headers |
103
|
|
|
{ |
104
|
|
|
return $this->defaultHeaders; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return null|string |
109
|
|
|
*/ |
110
|
|
|
public function getBaseUrl(): ?string |
111
|
|
|
{ |
112
|
|
|
return $this->baseUrl; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param null|string $baseUrl |
117
|
|
|
* |
118
|
|
|
* @return CurlRequestFactory |
119
|
|
|
*/ |
120
|
3 |
|
public function setBaseUrl(?string $baseUrl): self |
121
|
|
|
{ |
122
|
3 |
|
if ($baseUrl) { |
123
|
3 |
|
$baseUrl = rtrim($baseUrl, '/'); |
124
|
|
|
} |
125
|
|
|
|
126
|
3 |
|
$this->baseUrl = $baseUrl; |
127
|
|
|
|
128
|
3 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|