|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RattfieldNz\SafeUrls\Libraries\Curl; |
|
4
|
|
|
|
|
5
|
|
|
use Curl\Curl as PhpCurl; |
|
6
|
|
|
use RattfieldNz\SafeUrls\Libraries\Config\Config; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Curl. |
|
10
|
|
|
* |
|
11
|
|
|
* @category PHP |
|
12
|
|
|
* |
|
13
|
|
|
* @author Rob Attfield <[email protected]> |
|
14
|
|
|
* @license https://github.com/rattfieldnz/safe-urls/blob/master/license.md MIT |
|
15
|
|
|
* |
|
16
|
|
|
* @link https://github.com/rattfieldnz/safe-urls/ |
|
17
|
|
|
*/ |
|
18
|
|
|
class Curl |
|
19
|
|
|
{ |
|
20
|
|
|
private $_postUrl; |
|
21
|
|
|
private $_payload; |
|
22
|
|
|
private $_defaultHeaders; |
|
23
|
|
|
private $_timeout; |
|
24
|
|
|
private $_curl; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Curl constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* Set the needed properties to do a CURL request. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $postUrl URL to use for request. |
|
32
|
|
|
* @param array $payload Data to be submitted with CURL request. |
|
33
|
|
|
* @param int $timeout Timeout in seconds to complete a CURL request. Default is 10. |
|
34
|
|
|
* |
|
35
|
|
|
* @throws \ErrorException |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(string $postUrl, array $payload, int $timeout = 10) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->_curl = new PhpCurl(); |
|
40
|
|
|
$this->_postUrl = $postUrl; |
|
41
|
|
|
$this->_payload = $payload; |
|
42
|
|
|
$this->_timeout = $timeout; |
|
43
|
|
|
self::_setDefaultHeaders(); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Execute a CURL request, and return current object for further processing. |
|
48
|
|
|
* |
|
49
|
|
|
* @return PhpCurl |
|
50
|
|
|
*/ |
|
51
|
|
|
public function execute(): PhpCurl |
|
52
|
|
|
{ |
|
53
|
|
|
$this->_curl->setOpt(CURLOPT_RETURNTRANSFER, true); |
|
54
|
|
|
$this->_curl->setOpt(CURLOPT_CONNECTTIMEOUT, $this->_timeout); |
|
55
|
|
|
$this->_curl->setOpt(CURLOPT_HTTPHEADER, $this->_defaultHeaders); |
|
56
|
|
|
$this->_curl->setOpt(CURLOPT_POSTFIELDS, json_encode($this->_payload)); |
|
57
|
|
|
$this->_curl->post($this->_postUrl); |
|
58
|
|
|
return $this->_curl; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get the data retrieved from executing CURL request. |
|
63
|
|
|
* |
|
64
|
|
|
* @return array|string |
|
65
|
|
|
* @see \RattfieldNz\SafeUrls\Libraries\Curl\Curl->execute(). |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getData() |
|
68
|
|
|
{ |
|
69
|
|
|
$dataObject = $this->execute(); |
|
70
|
|
|
$data = [ |
|
71
|
|
|
'status' => $dataObject->getHttpStatus(), |
|
72
|
|
|
'response' => json_decode($dataObject->response, true) |
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
|
|
return json_encode($data); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Sets the default headers to use for CURL request. |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
private function _setDefaultHeaders(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$this->_defaultHeaders = [ |
|
86
|
|
|
'Content-Type: application/json', |
|
87
|
|
|
'Connection: Keep-Alive', |
|
88
|
|
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|