-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpRequest.php
69 lines (56 loc) · 2.19 KB
/
HttpRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
declare(strict_types=1);
namespace ContentSyndication {
use CurlHandle;
use Exception;
class HttpRequest
{
protected CurlHandle $curl;
protected mixed $content;
protected mixed $httpcode;
public function __construct(string $url)
{
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true); // follow redirects
curl_setopt($this->curl, CURLOPT_AUTOREFERER, true); // set referer on redirect
curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36 OPR/78.0.4093.112"); // some feeds require a user agent
curl_setopt($this->curl, CURLOPT_HEADER, false);
curl_setopt($this->curl, CURLOPT_TIMEOUT, 60);
curl_setopt($this->curl, CURLOPT_ENCODING, '');
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); // no echo, just return result
$this->content = curl_exec($this->curl);
if ($this->content === false) {
throw new Exception("CURL exception on: " . $url);
}
$this->httpcode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
if ($this->httpcode < 200 or $this->httpcode >= 400) {
throw new Exception("CURL http code $this->httpcode on: " . $url);
}
}
public function getHttpCode(): int
{
return (int)$this->httpcode;
}
public function getContent(): string
{
return $this->content;
}
public function getEffectiveUrl(): string
{
return curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL);
}
public function getContentType(): string
{
return curl_getinfo($this->curl, CURLINFO_CONTENT_TYPE);
}
public function getContentLength(): int
{
return (int)curl_getinfo($this->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
}
public function __destruct()
{
@curl_close($this->curl);
}
}
}