mirror of
https://github.com/twisterarmy/twister-phpbot.git
synced 2025-02-04 02:54:48 +00:00
support ETag and Last-Modified headers
This commit is contained in:
parent
d957239a1b
commit
639e5c8797
@ -4,9 +4,10 @@ mb_internal_encoding('UTF-8');
|
||||
setlocale(LC_CTYPE, 'en_US.UTF-8');
|
||||
|
||||
// Load RSS feed
|
||||
require_once 'rssreader.php';
|
||||
$feed_uri = 'http://habrahabr.ru/rss/hubs/new/';
|
||||
$rss = simplexml_load_file($feed_uri);
|
||||
if (!$rss) die('Cannot read rss');
|
||||
$rss = getRssFeed($feed_uri);
|
||||
if (!$rss) die('Cannot read rss or it is up to date');
|
||||
|
||||
// Initialise TwisterPost
|
||||
require_once 'twisterpost.php';
|
||||
|
53
rssreader.php
Normal file
53
rssreader.php
Normal file
@ -0,0 +1,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[$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);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user