forked from fossar/selfoss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimplePieFileGuzzle.php
52 lines (47 loc) · 2.02 KB
/
SimplePieFileGuzzle.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
<?php
namespace helpers;
/**
* Bridge to make SimplePie fetch resources using Guzzle library
*/
class SimplePieFileGuzzle extends \SimplePie_File {
public function __construct($url, $timeout = 10, $redirects = 5, $headers = [], $useragent = null, $force_fsockopen = false) {
$this->url = $url;
$this->permanent_url = $url;
$this->useragent = $useragent;
if (preg_match('/^https?:\/\//i', $url)) {
$this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_CURL;
$client = \helpers\WebClient::getHttpClient();
try {
$response = $client->get($url, [
'allow_redirects' => [
'max' => $redirects,
],
'headers' => [
'User-Agent' => $useragent,
'Referer' => $url,
] + $headers,
'timeout' => $timeout,
'connect_timeout' => $timeout,
'allow_redirects' => [
'track_redirects' => true,
],
]);
$this->headers = $response->getHeaders();
// Sequence of fetched URLs
$urlStack = [$url] + $response->getHeader(\GuzzleHttp\RedirectMiddleware::HISTORY_HEADER);
$this->url = $urlStack[count($urlStack) - 1];
$this->body = (string) $response->getBody();
$this->status_code = $response->getStatusCode();
} catch (\GuzzleHttp\Exception\RequestException $e) {
$this->error = $e->getMessage();
$this->success = false;
}
} else {
$this->method = SIMPLEPIE_FILE_SOURCE_LOCAL | SIMPLEPIE_FILE_SOURCE_FILE_GET_CONTENTS;
if (empty($url) || !($this->body = trim(file_get_contents($url)))) {
$this->error = 'file_get_contents could not read the file';
$this->success = false;
}
}
}
}