1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api; |
4
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Interfaces\HttpInterface; |
6
|
|
|
use seregazhuk\PinterestBot\Helpers\CsrfHelper; |
7
|
|
|
use seregazhuk\PinterestBot\Helpers\UrlHelper; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Http |
11
|
|
|
* |
12
|
|
|
* @package seregazhuk\PinterestBot |
13
|
|
|
* @property string $cookieJar |
14
|
|
|
* @property string $cookePath |
15
|
|
|
*/ |
16
|
|
|
class CurlDecorator implements HttpInterface |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
protected $http; |
20
|
|
|
protected $options; |
21
|
|
|
protected $loggedIn; |
22
|
|
|
|
23
|
|
|
public $cookieJar; |
24
|
|
|
public $cookiePath; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Contains the curl instance |
28
|
|
|
* |
29
|
|
|
* @var resource |
30
|
|
|
*/ |
31
|
|
|
private $curl; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Initializes curl resource |
35
|
|
|
* |
36
|
|
|
* @access public |
37
|
|
|
* @param string $url |
38
|
|
|
*/ |
39
|
|
|
public function init($url) |
40
|
|
|
{ |
41
|
|
|
$this->curl = curl_init($url); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Sets an option in the curl instance |
46
|
|
|
* |
47
|
|
|
* @access public |
48
|
|
|
* @param string $option |
49
|
|
|
* @param string $value |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function setOption($option, $value) |
53
|
|
|
{ |
54
|
|
|
curl_setopt($this->curl, $option, $value); |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Sets multiple options at the same time |
61
|
|
|
* |
62
|
|
|
* @access public |
63
|
|
|
* @param array $options |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function setOptions(array $options = []) |
67
|
|
|
{ |
68
|
|
|
curl_setopt_array($this->curl, $options); |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Check if the curl request ended up with errors |
76
|
|
|
* |
77
|
|
|
* @access public |
78
|
|
|
* @return boolean |
79
|
|
|
*/ |
80
|
|
|
public function hasErrors() |
81
|
|
|
{ |
82
|
|
|
return curl_errno($this->curl) ? true : false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get curl errors |
87
|
|
|
* |
88
|
|
|
* @access public |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getErrors() |
92
|
|
|
{ |
93
|
|
|
return curl_error($this->curl); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get curl info key |
98
|
|
|
* |
99
|
|
|
* @access public |
100
|
|
|
* @param string $key |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function getInfo($key) |
104
|
|
|
{ |
105
|
|
|
return curl_getinfo($this->curl, $key); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Close the curl resource |
110
|
|
|
* |
111
|
|
|
* @access public |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public function close() |
115
|
|
|
{ |
116
|
|
|
curl_close($this->curl); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Executes curl request |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public function execute() |
126
|
|
|
{ |
127
|
|
|
return curl_exec($this->curl); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|