1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Swis\PdfcrowdClient\Http; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use GuzzleHttp\RequestOptions; |
9
|
|
|
|
10
|
|
|
class GuzzleRequest implements RequestInterface |
11
|
|
|
{ |
12
|
|
|
/** @var \GuzzleHttp\Client */ |
13
|
|
|
protected $client; |
14
|
|
|
|
15
|
|
|
/** @var \GuzzleHttp\Psr7\Request */ |
16
|
|
|
protected $request; |
17
|
|
|
|
18
|
|
|
/** @var array */ |
19
|
|
|
protected $requestOptions = []; |
20
|
|
|
|
21
|
|
|
/** @var \GuzzleHttp\Psr7\Response */ |
22
|
|
|
protected $response; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
protected $url; |
26
|
|
|
|
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
$this->client = new Client(); |
30
|
|
|
|
31
|
|
|
$this->requestOptions[RequestOptions::CONNECT_TIMEOUT] = 10; |
32
|
|
|
$this->requestOptions[RequestOptions::HTTP_ERRORS] = false; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Override the client, used for testing purposes. |
37
|
|
|
* |
38
|
|
|
* @param \GuzzleHttp\Client $client |
39
|
|
|
*/ |
40
|
|
|
public function setClient(Client $client) |
41
|
|
|
{ |
42
|
|
|
$this->client = $client; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function setUserAgent(string $userAgentString) |
46
|
|
|
{ |
47
|
|
|
if (!isset($this->requestOptions[RequestOptions::HEADERS])) { |
48
|
|
|
$this->requestOptions[RequestOptions::HEADERS] = []; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->requestOptions[RequestOptions::HEADERS]['User-Agent'] = $userAgentString; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function setTimeout(float $timeout) |
55
|
|
|
{ |
56
|
|
|
$this->requestOptions[RequestOptions::TIMEOUT] = $timeout; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setVerifySsl(bool $verify) |
60
|
|
|
{ |
61
|
|
|
$this->requestOptions[RequestOptions::VERIFY] = $verify; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function setProxy(string $proxy, int $port) |
65
|
|
|
{ |
66
|
|
|
$this->requestOptions[RequestOptions::PROXY] = $proxy.':'.$port; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setProxyAuth(string $username, string $password) |
70
|
|
|
{ |
71
|
|
|
$this->requestOptions[RequestOptions::AUTH] = [ |
72
|
|
|
$username, |
73
|
|
|
$password, |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function setUrl(string $url) |
78
|
|
|
{ |
79
|
|
|
$this->url = $url; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function setBody(array $body) |
83
|
|
|
{ |
84
|
|
|
$this->requestOptions[RequestOptions::FORM_PARAMS] = $body; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function setOutputDestination($output_destination) |
88
|
|
|
{ |
89
|
|
|
$this->requestOptions[RequestOptions::SINK] = $output_destination; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function execute() |
93
|
|
|
{ |
94
|
|
|
$this->response = $this->client->request('POST', $this->url, $this->requestOptions); |
95
|
|
|
|
96
|
|
|
return $this->response->getBody(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getHttpStatusCode(): int |
100
|
|
|
{ |
101
|
|
|
return (int) $this->response->getStatusCode(); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|