1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PiedWeb\Curl; |
4
|
|
|
|
5
|
|
|
class Response |
6
|
|
|
{ |
7
|
|
|
private $exec; |
|
|
|
|
8
|
|
|
|
9
|
|
|
private $url; |
10
|
|
|
|
11
|
|
|
/** @var string * */ |
12
|
|
|
private $headers; |
13
|
|
|
/** @var string * */ |
14
|
|
|
private $content; |
15
|
|
|
/** @var array * */ |
16
|
|
|
private $info; |
17
|
|
|
|
18
|
15 |
|
public static function get($handle, string $url, int $returnHeaders) |
19
|
|
|
{ |
20
|
15 |
|
$content = curl_exec($handle); |
21
|
|
|
|
22
|
15 |
|
if (!$content) { |
23
|
13 |
|
return curl_errno($handle); |
24
|
|
|
} |
25
|
|
|
|
26
|
2 |
|
$self = new self(); |
27
|
|
|
|
28
|
2 |
|
if (2 === $returnHeaders) { |
29
|
|
|
$self->headers = $content; |
30
|
|
|
} else { |
31
|
2 |
|
if ($returnHeaders) { // Remove headerss from response |
32
|
2 |
|
$self->headers = substr($content, 0, $sHeaders = curl_getinfo($handle, CURLINFO_HEADER_SIZE)); |
33
|
2 |
|
$content = substr($content, $sHeaders); |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
$self->content = $content; |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
$self->info = curl_getinfo($handle); // curl_getinfo(self::$ch, CURLINFO_EFFECTIVE_URL) |
40
|
2 |
|
$self->url = $url; |
41
|
|
|
|
42
|
2 |
|
return $self; |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
private function __construct() |
46
|
|
|
{ |
47
|
2 |
|
} |
48
|
|
|
|
49
|
2 |
|
public function getContent() |
50
|
|
|
{ |
51
|
2 |
|
return $this->content; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Return headers's data return by the request. |
56
|
|
|
* |
57
|
|
|
* @param bool $returnArray True to get an array, false to get a string |
58
|
|
|
* |
59
|
|
|
* @return array|string|null containing headers's data |
60
|
|
|
*/ |
61
|
|
|
public function getHeaders(bool $returnArray = true) |
62
|
|
|
{ |
63
|
|
|
if (isset($this->headers)) { |
64
|
|
|
return true === $returnArray ? Helper::httpParseHeaders($this->headers) : $this->headers; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string requested url |
70
|
|
|
*/ |
71
|
1 |
|
public function getUrl() |
72
|
|
|
{ |
73
|
1 |
|
return $this->url; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Return current effective url. |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
2 |
|
public function getEffectiveUrl() |
82
|
|
|
{ |
83
|
2 |
|
return isset($this->info['url']) ? $this->info['url'] : null; //curl_getinfo(self::$ch, CURLINFO_EFFECTIVE_URL); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return the cookie(s) returned by the request (if there are). |
88
|
|
|
* |
89
|
|
|
* @return null|array containing the cookies |
90
|
|
|
*/ |
91
|
|
|
public function getCookies() |
92
|
|
|
{ |
93
|
|
|
if (isset($this->headers)) { |
94
|
|
|
$headers = $this->getHeaders(); |
95
|
|
|
if (isset($headers['Set-Cookie'])) { |
96
|
|
|
return is_array($headers['Set-Cookie']) ? implode('; ', $headers['Set-Cookie']) : $headers['Set-Cookie']; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get information regarding the request. |
103
|
|
|
* |
104
|
|
|
* @return array an associative array with the following elements (which correspond to opt), or FALSE on failure |
105
|
|
|
*/ |
106
|
|
|
public function getInfo() |
107
|
|
|
{ |
108
|
|
|
return $this->info; |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
public function getStatusCode() |
112
|
|
|
{ |
113
|
1 |
|
return $this->info['http_code']; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getContentType() |
117
|
|
|
{ |
118
|
|
|
return $this->info['content_type']; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|