Total Complexity | 6 |
Total Lines | 78 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
11 | class Client |
||
12 | { |
||
13 | const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'; |
||
14 | |||
15 | /** |
||
16 | * @var resource |
||
17 | */ |
||
18 | protected $curl; |
||
19 | |||
20 | /** |
||
21 | * Initializes the cURL session. |
||
22 | */ |
||
23 | 15 | public function __construct() |
|
24 | { |
||
25 | 15 | $curl = curl_init(); |
|
26 | |||
27 | 15 | curl_setopt($curl, CURLOPT_ENCODING, ''); |
|
|
|||
28 | |||
29 | 15 | curl_setopt($curl, CURLOPT_USERAGENT, self::USER_AGENT); |
|
30 | |||
31 | 15 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
|
32 | |||
33 | 15 | $this->curl = $curl; |
|
34 | 15 | } |
|
35 | |||
36 | /** |
||
37 | * Performs the cURL session. |
||
38 | * |
||
39 | * @param boolean $close |
||
40 | * @return mixed |
||
41 | */ |
||
42 | 15 | public function execute($close = true) |
|
43 | { |
||
44 | 15 | $result = curl_exec($this->curl); |
|
45 | |||
46 | 15 | $close && curl_close($this->curl); |
|
47 | |||
48 | 15 | return $result; |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * Sets an option to the cURL session. |
||
53 | * |
||
54 | * @param integer $key |
||
55 | * @param mixed $value |
||
56 | * @return self |
||
57 | */ |
||
58 | 15 | public function set($key, $value) |
|
59 | { |
||
60 | 15 | curl_setopt($this->curl, $key, $value); |
|
61 | |||
62 | 15 | return $this; |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * Sets the URL to be used in the cURL session. |
||
67 | * |
||
68 | * @param string $link |
||
69 | * @return self |
||
70 | */ |
||
71 | 15 | public function url($link) |
|
72 | { |
||
73 | 15 | return $this->set(CURLOPT_URL, $link); |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * Performs a new cURL session with an URL. |
||
78 | * |
||
79 | * @param string $url |
||
80 | * @return string |
||
81 | */ |
||
82 | 15 | public static function request($url) |
|
89 | } |
||
90 | } |
||
91 |