1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fhp\Adapter; |
4
|
|
|
|
5
|
|
|
use Fhp\Adapter\Exception\AdapterException; |
6
|
|
|
use Fhp\Adapter\Exception\CurlException; |
7
|
|
|
use Fhp\Message\AbstractMessage; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Curl |
11
|
|
|
* @package Fhp\Adapter |
12
|
|
|
*/ |
13
|
|
|
class Curl implements AdapterInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $host; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
protected $port; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var resource |
27
|
|
|
*/ |
28
|
|
|
protected $curlHandle; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var mixed |
32
|
|
|
*/ |
33
|
|
|
protected $lastResponseInfo; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Curl constructor. |
37
|
|
|
* |
38
|
|
|
* @param string $host |
39
|
|
|
* @param int $port |
40
|
|
|
* @throws AdapterException |
41
|
|
|
* @throws CurlException |
42
|
|
|
*/ |
43
|
|
|
public function __construct($host, $port) |
44
|
|
|
{ |
45
|
|
|
if (!is_integer($port) || (int) $port <= 0) { |
46
|
|
|
throw new AdapterException('Invalid port number'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->host = (string) $host; |
50
|
|
|
$this->port = (int) $port; |
51
|
|
|
$this->curlHandle = curl_init(); |
52
|
|
|
|
53
|
|
|
curl_setopt($this->curlHandle, CURLOPT_SSLVERSION, 1); |
54
|
|
|
curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, true); |
55
|
|
|
curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYHOST, 2); |
56
|
|
|
curl_setopt($this->curlHandle, CURLOPT_USERAGENT, "FHP-lib"); |
57
|
|
|
curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true); |
58
|
|
|
curl_setopt($this->curlHandle, CURLOPT_URL, $this->host); |
59
|
|
|
curl_setopt($this->curlHandle, CURLOPT_CONNECTTIMEOUT, 15); |
60
|
|
|
curl_setopt($this->curlHandle, CURLOPT_CUSTOMREQUEST, 'POST'); |
61
|
|
|
curl_setopt($this->curlHandle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); |
62
|
|
|
curl_setopt($this->curlHandle, CURLOPT_ENCODING, ''); |
63
|
|
|
curl_setopt($this->curlHandle, CURLOPT_MAXREDIRS, 0); |
64
|
|
|
curl_setopt($this->curlHandle, CURLOPT_TIMEOUT, 30); |
65
|
|
|
curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, array("cache-control: no-cache", 'Content-Type: text/plain')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param AbstractMessage $message |
70
|
|
|
* @return string |
71
|
|
|
* @throws CurlException |
72
|
|
|
*/ |
73
|
|
|
public function send(AbstractMessage $message) |
74
|
|
|
{ |
75
|
|
|
curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, base64_encode($message->toString())); |
76
|
|
|
$response = curl_exec($this->curlHandle); |
77
|
|
|
$this->lastResponseInfo = curl_getinfo($this->curlHandle); |
78
|
|
|
|
79
|
|
|
if (false === $response) { |
80
|
|
|
throw new CurlException( |
81
|
|
|
'Failed connection to ' . $this->host . ': ' . curl_error($this->curlHandle), |
82
|
|
|
curl_errno($this->curlHandle), |
83
|
|
|
null, |
84
|
|
|
$this->lastResponseInfo |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$statusCode = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE); |
89
|
|
|
|
90
|
|
|
if ($statusCode < 200 || $statusCode > 299) { |
91
|
|
|
throw new CurlException('Bad response with status code ' . $statusCode, 0, null, $this->lastResponseInfo); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return base64_decode($response); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Gets curl info for last request / response. |
99
|
|
|
* |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
public function getLastResponseInfo() |
103
|
|
|
{ |
104
|
|
|
return $this->lastResponseInfo; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|