1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Api; |
4
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Helpers\UrlHelper; |
6
|
|
|
use seregazhuk\PinterestBot\Helpers\CsrfHelper; |
7
|
|
|
use seregazhuk\PinterestBot\Api\Contracts\HttpClient; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class CurlAdapter. |
11
|
|
|
*/ |
12
|
|
|
class CurlHttpClient implements HttpClient |
13
|
|
|
{ |
14
|
|
|
const COOKIE_NAME = 'pinterest_cookie'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $options; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $userAgent = 'Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $headers; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Contains the curl instance. |
33
|
|
|
* |
34
|
|
|
* @var resource |
35
|
|
|
*/ |
36
|
|
|
protected $curl; |
37
|
|
|
protected $cookieJar; |
38
|
|
|
|
39
|
|
|
public function __construct() |
40
|
|
|
{ |
41
|
|
|
$this->cookieJar = tempnam(sys_get_temp_dir(), self::COOKIE_NAME); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Executes curl request. |
46
|
|
|
* |
47
|
|
|
* @param string $url |
48
|
|
|
* @param string $postString |
49
|
|
|
* @param array $headers |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function execute($url, $postString, array $headers = []) |
53
|
|
|
{ |
54
|
|
|
$this->headers = $headers; |
55
|
|
|
$this->init($url)->setOptions($postString); |
56
|
|
|
|
57
|
|
|
$res = curl_exec($this->curl); |
58
|
|
|
$this->close(); |
59
|
|
|
|
60
|
|
|
return $res; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get curl errors. |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getErrors() |
69
|
|
|
{ |
70
|
|
|
return curl_error($this->curl); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return null|string |
75
|
|
|
*/ |
76
|
|
|
public function getToken() |
77
|
|
|
{ |
78
|
|
|
return CsrfHelper::getTokenFromFile($this->cookieJar); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $userAgent |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function setUserAgent($userAgent) |
86
|
|
|
{ |
87
|
|
|
if ($userAgent !== null) { |
88
|
|
|
$this->userAgent = $userAgent; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Close the curl resource. |
96
|
|
|
* |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
protected function close() |
100
|
|
|
{ |
101
|
|
|
curl_close($this->curl); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Initializes curl resource. |
106
|
|
|
* |
107
|
|
|
* @param string $url |
108
|
|
|
* |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
|
|
protected function init($url) |
112
|
|
|
{ |
113
|
|
|
$this->curl = curl_init($url); |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Sets multiple options at the same time. |
120
|
|
|
* |
121
|
|
|
* @param string $postString |
122
|
|
|
* @return static |
123
|
|
|
*/ |
124
|
|
|
protected function setOptions($postString) |
125
|
|
|
{ |
126
|
|
|
$this->makeHttpOptions($postString); |
127
|
|
|
|
128
|
|
|
curl_setopt_array($this->curl, $this->options); |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
protected function setDefaultHttpOptions() |
137
|
|
|
{ |
138
|
|
|
$this->options = [ |
139
|
|
|
CURLOPT_USERAGENT => $this->userAgent, |
140
|
|
|
CURLOPT_RETURNTRANSFER => true, |
141
|
|
|
CURLOPT_SSL_VERIFYPEER => false, |
142
|
|
|
CURLOPT_FOLLOWLOCATION => true, |
143
|
|
|
CURLOPT_ENCODING => 'gzip,deflate', |
144
|
|
|
CURLOPT_HTTPHEADER => $this->headers, |
145
|
|
|
CURLOPT_REFERER => UrlHelper::URL_BASE, |
146
|
|
|
CURLOPT_COOKIEFILE => $this->cookieJar, |
147
|
|
|
CURLOPT_COOKIEJAR => $this->cookieJar, |
148
|
|
|
]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Adds necessary curl options for query. |
153
|
|
|
* |
154
|
|
|
* @param string $postString POST query string |
155
|
|
|
* |
156
|
|
|
* @return $this |
157
|
|
|
*/ |
158
|
|
|
protected function makeHttpOptions($postString = '') |
159
|
|
|
{ |
160
|
|
|
$this->setDefaultHttpOptions(); |
161
|
|
|
|
162
|
|
|
if (!empty($postString)) { |
163
|
|
|
$this->options[CURLOPT_POST] = true; |
164
|
|
|
$this->options[CURLOPT_POSTFIELDS] = $postString; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|