Php 瞬态API缓存中的数据共享计数不准确
嗨,我有一个wp multisite,在那里我使用Transients API缓存社交媒体共享计数。我用的是贴在这里的答案: 一切都在运行,但它并没有给我所有帖子的准确份额计数。有些人的股数正确,有些人只是显示一个随机数。例如,一篇有65个facebook喜欢的帖子在添加临时代码时只显示1。当我删除瞬态时,它会显示所有瞬态的准确股数。你知道这是什么原因吗 下面是我添加到functions.php的代码:Php 瞬态API缓存中的数据共享计数不准确,php,wordpress,caching,transient,Php,Wordpress,Caching,Transient,嗨,我有一个wp multisite,在那里我使用Transients API缓存社交媒体共享计数。我用的是贴在这里的答案: 一切都在运行,但它并没有给我所有帖子的准确份额计数。有些人的股数正确,有些人只是显示一个随机数。例如,一篇有65个facebook喜欢的帖子在添加临时代码时只显示1。当我删除瞬态时,它会显示所有瞬态的准确股数。你知道这是什么原因吗 下面是我添加到functions.php的代码: 类共享计数{ 私有$url,$timeout; 函数构造($url,$timeout=10
类共享计数{
私有$url,$timeout;
函数构造($url,$timeout=10){
$this->url=rawurlencode($url);
$this->timeout=$timeout;
}
函数get_fb(){
$json\u string=$this->file\u get\u contents\u curl('http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls=“.$this->url);
$json=json_解码($json_字符串,true);
返回isset($json[0]['total_count'])?intval($json[0]['total_count']):0;
}
私有函数文件\u get\u contents\u curl($url){
//创建唯一的临时密钥
$transientKey='sc_uu'+md5($url);
//检查缓存
$cache=get\u site\u transient($transientKey);
如果($cache){
返回$cache;
}
否则{
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT');
curl_setopt($ch,CURLOPT_FAILONERROR,1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,$this->TIMEOUT);
$count=curl\u exec($ch);
if(旋度误差($ch))
{
模具(旋度误差($ch));
}
//缓存结果1小时
设置站点瞬态($transientKey,$count,60*60);
返回$count;
}
}
}
我使用了这个片段,它为ShareCountAPI做了工作
function aesop_share_count(){
$post_id = get_the_ID();
//$url = 'http://nickhaskins.co'; // this one used for testing to return a working result
$url = get_permalink();
$apiurl = sprintf('http://api.sharedcount.com/?url=%s',$url);
$transientKey = 'AesopShareCounts'. (int) $post_id;
$cached = get_transient($transientKey);
if (false !== $cached) {
return $cached;
}
$fetch = wp_remote_get($apiurl, array('sslverify'=>false));
$remote = wp_remote_retrieve_body($fetch);
if( !is_wp_error( $remote ) ) {
$count = json_decode( $remote,true);
}
$twitter = $count['Twitter'];
$fb_like = $count['Facebook']['like_count'];
$total = $fb_like + $twitter;
$out = sprintf('%s',$total);
set_transient($transientKey, $out, 600);
return $out;
}
嗨,丹尼尔,你用瞬变电磁法找到解决方案了吗?