Completed
Push — master ( e9ef07...05c89c )
by Sergey
02:57 queued 23s
created

CurlHttpClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api;
4
5
use seregazhuk\PinterestBot\Api\Contracts\HttpClient;
6
use seregazhuk\PinterestBot\Helpers\Cookies;
7
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
8
9
/**
10
 * Class CurlAdapter.
11
 */
12
class CurlHttpClient implements HttpClient
13
{
14
    /**
15
     * Custom CURL options for requests.
16
     *
17
     * @var array
18
     */
19
    protected $options = [
20
        CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0'
21
    ];
22
23
    /**
24
     * @var array
25
     */
26
    protected $headers = [];
27
28
    /**
29
     * Contains the curl instance.
30
     *
31
     * @var resource
32
     */
33
    protected $curl;
34
35
    /**
36
     * @var string
37
     */
38
    protected $cookieJar;
39
40
    /**
41
     * Cookies container
42
     *
43
     * @var Cookies
44
     */
45
    protected $cookies;
46
47
    public function __construct(Cookies $cookies)
48
    {
49
        $this->cookies = $cookies;
50
    }
51
52
    /**
53
     * Load cookies for specified username
54
     *
55
     * @param string $username
56
     * @return HttpClient
57
     */
58
    public function loadCookies($username = '')
59
    {
60
        $this->initCookieJar($username);
61
        $this->cookies->fill($this->cookieJar);
62
63
        return $this;
64
    }
65
66
    /**
67
     * Executes curl request.
68
     *
69
     * @param string $url
70
     * @param string $postString
71
     * @param array $headers
72
     * @return string
73
     */
74
    public function execute($url, $postString = '', array $headers = [])
75
    {
76
        $this->headers = $headers;
77
78
        $this->init($url, $postString);
79
80
        $res = curl_exec($this->curl);
81
        $this->close();
82
83
        $this->cookies->fill($this->cookieJar);
84
85
        return $res;
86
    }
87
88
    /**
89
     * Get curl errors.
90
     *
91
     * @return string
92
     */
93
    public function getErrors()
94
    {
95
        return curl_error($this->curl);
96
    }
97
98
    /**
99
     * Close the curl resource.
100
     *
101
     * @return void
102
     */
103
    protected function close()
104
    {
105
        curl_close($this->curl);
106
    }
107
108
    /**
109
     * Initializes curl resource with options.
110
     *
111
     * @param string $url
112
     * @param $postString
113
     * @return $this
114
     */
115
    protected function init($url, $postString)
116
    {
117
        $this->curl = curl_init($url);
118
119
        if (empty($this->cookieJar)) {
120
            $this->loadCookies();
121
        }
122
123
        curl_setopt_array($this->curl, $this->makeHttpOptions($postString));
124
125
        return $this;
126
    }
127
128
    /**
129
     * @return array
130
     */
131
    protected function getDefaultHttpOptions()
132
    {
133
        return [
134
            CURLOPT_RETURNTRANSFER => true,
135
            CURLOPT_SSL_VERIFYPEER => false,
136
            CURLOPT_FOLLOWLOCATION => true,
137
            CURLOPT_ENCODING       => 'gzip,deflate',
138
            CURLOPT_HTTPHEADER     => $this->headers,
139
            CURLOPT_REFERER        => UrlBuilder::URL_BASE,
140
            CURLOPT_COOKIEFILE     => $this->cookieJar,
141
            CURLOPT_COOKIEJAR      => $this->cookieJar,
142
        ];
143
    }
144
145
    /**
146
     * Adds necessary curl options for query.
147
     *
148
     * @param string $postString POST query string
149
     *
150
     * @return array
151
     */
152
    protected function makeHttpOptions($postString = '')
153
    {
154
        // Union custom Curl options and default.
155
        $options = array_replace(
156
            $this->options,
157
            $this->getDefaultHttpOptions()
158
        );
159
160
        if (!empty($postString)) {
161
            $options[CURLOPT_POST] = true;
162
            $options[CURLOPT_POSTFIELDS] = $postString;
163
        }
164
165
        return $options;
166
    }
167
168
    /**
169
     * Set custom Curl options to override default
170
     *
171
     * @param array $options
172
     * @return CurlHttpClient
173
     */
174
    public function setOptions(array $options)
175
    {
176
        $this->options = $options;
177
178
        return $this;
179
    }
180
181
    /**
182
     * Get a cookie value by name
183
     *
184
     * @param $name
185
     * @return mixed
186
     */
187
    public function cookie($name)
188
    {
189
        return $this->cookies->get($name);
190
    }
191
192
    /**
193
     * Get all cookies
194
     *
195
     * @return array
196
     */
197
    public function cookies()
198
    {
199
        return $this->cookies->all();
200
    }
201
202
    /**
203
     * Init cookie file for a specified username. If username is empty we use
204
     * common cookie file for all sessions. If file does not exist it will
205
     * be created in system temp directory.
206
     *
207
     * @param $username
208
     * @return $this
209
     */
210
    protected function initCookieJar($username = '')
211
    {
212
        $cookieName = 'printerest_cookie_' . $username;
213
        $cookieFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $cookieName;
214
215
        if (!file_exists($cookieFilePath)) {
216
            touch($cookieFilePath);
217
        }
218
219
        $this->cookieJar = $cookieFilePath;
220
221
        return $this;
222
    }
223
}