|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ComicVine\Api\Connection; |
|
4
|
|
|
|
|
5
|
|
|
use ComicVine\Exceptions\InvalidUrl; |
|
6
|
|
|
use ComicVine\Exceptions\EmptyResponse; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Extending Connection contract. Make connection |
|
11
|
|
|
* to ComicVine using cURL. |
|
12
|
|
|
* |
|
13
|
|
|
* Class CURLConnection |
|
14
|
|
|
* |
|
15
|
|
|
* @package grzgajda/comicvine-api |
|
16
|
|
|
* @author Grzegorz Gajda <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class CURLConnection implements Connection |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var resource cURL resource |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $curl; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var mixed cURL response |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $response; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string Default user agent. |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Make cURL instance and set default values. |
|
37
|
|
|
* |
|
38
|
|
|
* @param int $timeout Duration of connection. |
|
39
|
|
|
* @param string $userAgent User agent (default is Mozilla) |
|
40
|
|
|
* |
|
41
|
|
|
* @return $this |
|
42
|
|
|
*/ |
|
43
|
|
|
public function makeConnection($timeout = 30, $userAgent = "") |
|
44
|
|
|
{ |
|
45
|
|
|
if ($userAgent === "") { |
|
46
|
|
|
$userAgent = $this->userAgent; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// Init cURL |
|
50
|
|
|
$this->curl = curl_init(); |
|
51
|
|
|
// cURL settings |
|
52
|
|
|
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); |
|
53
|
|
|
curl_setopt($this->curl, CURLOPT_TIMEOUT, $timeout); |
|
54
|
|
|
curl_setopt($this->curl, CURLOPT_USERAGENT, $userAgent); |
|
55
|
|
|
|
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Make an URL for connection. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $url Url address for request. |
|
63
|
|
|
* |
|
64
|
|
|
* @return $this |
|
65
|
|
|
* @throws \ComicVine\Exceptions\InvalidUrl |
|
66
|
|
|
*/ |
|
67
|
|
|
public function setConnection($url) |
|
68
|
|
|
{ |
|
69
|
|
|
if (filter_var($url, FILTER_VALIDATE_URL) === false) { |
|
70
|
|
|
throw new InvalidUrl("Passed wrong URL"); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// set cURL url address |
|
74
|
|
|
curl_setopt($this->curl, CURLOPT_URL, $url); |
|
75
|
|
|
// exec the request |
|
76
|
|
|
$this->response = curl_exec($this->curl); |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get status code of cURL request. |
|
83
|
|
|
* |
|
84
|
|
|
* @return mixed cURL status code |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getHttpStatus() |
|
87
|
|
|
{ |
|
88
|
|
|
return curl_getinfo($this->curl, CURLINFO_HTTP_CODE); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Return response from cURL request. |
|
93
|
|
|
* |
|
94
|
|
|
* @return mixed Response of request |
|
95
|
|
|
* @throws \ComicVine\Exceptions\EmptyResponse |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getResult() |
|
98
|
|
|
{ |
|
99
|
|
|
if (empty($this->response) === true) { |
|
100
|
|
|
throw new EmptyResponse("Request has not been executed."); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $this->response; |
|
104
|
|
|
} |
|
105
|
|
|
} |