开放的编程资料库

当前位置:我爱分享网 > PHP教程 > 正文

检索、缓存和显示您的 FeedBurner 订阅者数量

显着加快您的网站速度的一种方法是缓存您从其他网站或数据库远程检索的信息。缓存信息非常简单,可以节省用户不必要的等待时间。以下是我在网站上缓存我的 FeedBurner 关注者数量的方法。

PHP

/* settings */
$cache_path = '/cache/';
$rss_file_name = date('Y-m-d').'-rss.txt';

/* rss */
if(file_exists($cache_path.$rss_file_name)) {
	$rss_subcribers = file_get_contents($cache_path.$rss_file_name);
}
else {
	$rss_content = get_url('https://feedburner.google.com/api/awareness/1.0/GetFeedData?id=dfadajf324789fhSDFDf48');
	$subscribers = get_match('/circulation="(.*)"/isU',$rss_content);
	if($subscribers) {
		$subscribers = number_format($subscribers,0,'',',');
		file_put_contents($cache_path.$rss_file_name,$subscribers);
		$rss_subcribers = $subscribers;
	}
}

/* display */
echo 'My subscriber count is: ',$rss_subscribers;

/* connects to the URL, returns content */
function get_url($url) {
	$ch = curl_init();
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
	$content = curl_exec($ch);
	curl_close($ch);
	return $content;
}

/* helper: does the regex */
function get_match($regex,$content) {
	preg_match($regex,$content,$matches);
	return $matches[1];
}

我们做的第一件事是检查我们的缓存文件是否存在。如果是,我们简单地返回它的内容。如果缓存文件不存在,我们将连接到我们的私有 FeedBurner URL,解析内容,并将提要保存到以当天命名的文件中。请注意,我们可以通过以“年-月-日”格式命名文件来检查我们是否获得了当天的计数。

未经允许不得转载:我爱分享网 » 检索、缓存和显示您的 FeedBurner 订阅者数量

感觉很棒!可以赞赏支持我哟~

赞(0) 打赏