-
Notifications
You must be signed in to change notification settings - Fork 1
/
rssreader.php
53 lines (45 loc) · 1.54 KB
/
rssreader.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
<?php
function getRssFeed($feed_uri, $reload = false)
{
$meta_file = md5($feed_uri) . '.json';
$meta = @json_decode(@file_get_contents($meta_file), true);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $feed_uri);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if (!$reload) {
if (isset($meta['etag']))
curl_setopt($ch, CURLOPT_HTTPHEADER, array('If-None-Match: ' . $meta['etag']));
if (isset($meta['lastModified']))
curl_setopt($ch, CURLOPT_HTTPHEADER, array('If-Modified-Since: ' . $meta['lastModified']));
}
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$http_code = $info['http_code'];
if ($http_code !== 200)
return false;
$headers = substr($response, 0, $info['header_size']);
$headers_arr = array();
foreach (explode("\n", $headers) as $h) {
$h = explode(':', $h, 2);
if (!isset($h[1])) continue;
$key = $h[0];
$value = trim($h[1]);
if (!isset($headers_arr[$key]))
$headers_arr[$key] = $value;
elseif (is_array($headers_arr[$key]))
$headers_arr[$key][] = $value;
else
$headers_arr[$key] = array($headers_arr[$key], $value);
}
$meta = array();
if (isset($headers_arr['Etag']))
$meta['etag'] = $headers_arr['Etag'];
if (isset($headers_arr['Last-Modified']))
$meta['lastModified'] = $headers_arr['Last-Modified'];
file_put_contents($meta_file, json_encode($meta));
$body = substr($response, $info['header_size']);
return @simplexml_load_string($body);
}