我的网站上有一些自定义社交按钮,我使用 API 中的 json 获取分享号/关注者号。我试图实现一个缓存系统来减少加载时间并消除因过度使用 API 而被“警告”的风险。但是,我在这方面没有成功,主要是因为我不太了解集成步骤。我希望有人可以帮助我集成缓存系统。
以下是 Twitter、Google Plus 和 Instagram 的 php 代码:
- 推特
ob_start(); $twittershare = 'http://cdn.api.twitter.com/1/urls/count.json?url='.$product["href"] .''; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $twittershare); curl_setopt($ch, CURLOPT_HEADER, 0); $jsonstring = curl_exec($ch); curl_close($ch); $bufferstr = ob_get_contents(); ob_end_clean(); $json = json_decode($bufferstr); 回声$json->计数;
- 谷歌加
$url = ''.$product["href"] .''; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://clients6.google.com/rpc?key=xxxxxxxxxx"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion" :"v1"}]'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $curl_results = curl_exec($ch); curl_close($ch); $json = json_decode($curl_results, true); $count = intval($json[0]['result']['metadata']['globalCounts']['count']); $数据 = 数组(); $data['plus_count'] = (string) $count; $data['url'] = $url; 回声 $data['plus_count'];
- Instagram(获取关注者号码)
ob_start(); $insta = 'https://api.instagram.com/v1/users/00000000?access_token={token}'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $insta); curl_setopt($ch, CURLOPT_HEADER, 0); $jsonstring = curl_exec($ch); curl_close($ch); $bufferstr = ob_get_contents(); ob_end_clean(); $json = json_decode($bufferstr); echo $json->data->counts->followed_by;
希望大家可以一步步指导我如何为上面的代码片段实现缓存系统。