function s_badge_new($uid, $bid, $username, $password) { if (s_bad_id($uid) || s_bad_string($username) || s_bad_string($password)) { return s_err_arg(); } $key = 'badge_new_by#' . $uid . $bid . $username . $password; if (false === ($data = s_memcache($key))) { $data = array('source' => APP_KEY, 'badge_id' => $bid, 'uids' => $uid, '_username' => $username, '_password' => $password); if ($data = s_badge_http('http://i2.api.weibo.com/2/proxy/badges/issue.json', $data, 'post')) { //缓存一小时 s_memcache($key, $data, 3600); } } return $data; }
function s_user_by_uid($uid) { if (s_bad_id($uid)) { return false; } $key = "user_by_uid#" . $uid; if (false === ($user = s_memcache($key))) { $arr = array("uid" => $uid); if (false === ($user = s_weibo_http("https://api.weibo.com/2/users/show.json", $arr))) { return false; } //存储memcache中 } return $user; }
function s_badge_new($uid, $bid, $username, $password) { if (s_bad_id($uid) || s_bad_string($username) || s_bad_string($password)) { return s_err_arg(); } $key = "badge_new_by#uid={$uid}&bid={$bid}&user={$username}&password={$password}"; if (false === ($data = s_memcache($key))) { $data = array('badge_id' => $bid, 'uids' => $uid, '_username' => $username, '_password' => $password); if (false === ($data = s_badge_http('http://api.t.sina.com.cn/badges/app/issue.json?source=' . APP_KEY, $data, 'post'))) { return s_err_sdk(); } //缓存一小时 s_memcache($key, $data, 300); } return $data; }
function s_badge_new($uid, $bid, $username, $password) { if (s_bad_id($uid) || s_bad_string($username) || s_bad_string($password)) { return s_err_arg(); } $key = "badge_new_by#uid={$uid}&bid={$bid}&user={$username}&password={$password}"; if (false === ($data = s_memcache($key))) { $data = array('badge_id' => $bid, 'uids' => $uid, '_username' => $username, '_password' => $password); //if (false === ( $data = s_badge_http('http://api.weibo.com/2/proxy/badges/issue.json', $data, 'post') )) { if (false === ($data = s_badge_http('http://i2.api.weibo.com/2/proxy/badges/issue.json', $data, 'post'))) { return s_err_sdk(); } //缓存五分钟 s_memcache($key, $data, 300); } return $data; }
function s_weibo_list_by_uid($uid, $page = 1, $count = 20) { if (s_bad_id($uid) || s_bad_id($page) || s_bad_id($count) || $count > 200) { return false; } //看cache中是否存在 $key = "weibo_list_by_uid#" . $uid . $page . $count; if (false === ($data = s_memcache($key))) { //缓存中没有,请求服务器 $params = array("user_id" => $uid, "count" => $count, "page" => $page); if (false === ($data = s_weibo_http('http://api.t.sina.com.cn/statuses/user_timeline.json', $params))) { return false; } //缓存起来900秒(15分钟) //$mem->set($key, $data, 0, 900); } return $data; }
function s_string_face(&$weibo) { if (s_bad_string($weibo)) { return false; } //获取表情列表 $mkey = 'face_get'; if (!($rep = s_memcache($mkey))) { //未在缓存中存在 if ($rep = s_http_response('https://api.weibo.com/2/emotions.json?source=1362404091&_=1365667265344')) { //缓存1天 s_memcache($mkey, $rep, 86400); } } // $json = json_decode($rep); return $weibo; //return preg_replace("/\#(.*)\#/iUs", "<a href=\"http://s.weibo.com/weibo/$1\" target=\"_blank\">$0</a>", $weibo); }
function s_live_now(&$user, $type = 1, $page = 1, $max = 10) { if (s_bad_array($user) || s_bad_id($page) || s_bad_id($type)) { return s_err_arg(); } $key = 'live_list_by#' . $page . $type . $max; if (false === ($data = s_memcache($key))) { $data = array('uid' => $user['uid'], 'type' => $type, 'page' => $page, 'pagesize' => $max); if (false === ($data = s_live_http('http://i.service.t.sina.com.cn/sapps/live/getlivelist.php', $data))) { return s_err_sdk(); } //缓存60秒 s_memcache($key, $data, 60); } return $data; }
function s_user_friends($uid, $count = 200, $page = 1) { if (s_bad_id($count) || s_bad_id($page)) { return s_err_arg(); } if (!s_bad_id($uid)) { //微博ID $data['uid'] = $uid; } else { if (!s_bad_string($uid)) { //微博昵称 $data['screen_name'] = $uid; } } $data['count'] = $count > 5000 ? 200 : $count; //游标从0开始 $data['cursor'] = $page - 1; $key = "user_followers_by_uid#{$uid}_{$count}_{$page}"; if (false !== ($users = s_memcache($key))) { return $users; } //缓存中没有,从微博平台中获取 if (false === ($ret = s_weibo_http("https://api.weibo.com/2/friendships/followers.json", $data)) || s_bad_array($ret['users'])) { return false; } $users = s_user_sample($ret['users']); //缓存中存储起来 s_memcache($key, $users); return $users; }
function s_weibo_surl($url) { if (s_bad_string($url)) { return false; } //看cache中是否存在 $mkey = 'weibo_surl#surl=' . $url; if (false === ($data = s_memcache($mkey))) { //缓存中没有,请求服务器 $params = array('url_long' => $url); if (false === ($data = s_weibo_http('https://api.weibo.com/2/short_url/shorten.json', $params))) { return false; } //缓存起来一天 s_memcache($mkey, $data, 86400); } return $data; }
function s_weibo_2id_by_mids($mids, $type = 1) { $is_array = 0; if (is_array($mids)) { sort($mids, SORT_STRING); $mids = implode(',', $mids); $is_array = 1; } if (s_bad_string($mids)) { return false; } //看cache中是否存在 $key = "weibo_2id_by_mids#" . $mids; if (false === ($data = s_memcache($key))) { //缓存中没有,请求服务器 $params = array("mid" => $mids, "type" => $type, "is_batch" => $is_array, "isBase62" => 1); if (false === ($data = s_weibo_http('https://api.weibo.com/2/statuses/queryid.json', $params))) { return false; } //缓存起来24小时 s_memcache($key, $data, 24 * 3600); } if (!$is_array) { //非数组查询,只返回对应的id return $data['id']; } $ret = array(); //数组需要处理下 foreach ($data as &$item) { $ret = array_merge($ret, $item); unset($item); } unset($data); return $ret; }
function s_user_weibo_ids($uid, $type = 0, $since = 0, $max = 0) { if (s_bad_id($uid) || s_bad_0id($max) || s_bad_0id($since) || s_bad_0id($type)) { return false; } $page = 1; $ret = array(); while ($page > 0) { $key = 'user_weibo_forward_ids_by#' . 'uid=' . $uid . 'type=' . $type . 'since_id=' . $since . 'max_id=' . $max; if (false === ($data = s_memcache($key))) { $param = array('uid' => $uid, 'since_id' => $since_id, 'max_id' => $since_id, 'page' => $page, 'feature' => $type); //if ( false === ( $data = s_weibo_http("http://i2.api.weibo.com/2/statuses/user_timeline/ids.json", $data) )) { if (false === ($data = s_weibo_http("https://api.weibo.com/2/statuses/user_timeline/ids.json", $data))) { return false; } //缓存中存储30秒 s_memcache($key, $data, 30); } $ret = array_merge($ret, $data['statuses']); $page = $data['next_cursor']; } return $ret; }
function s_user_ship($source, $target) { if (is_array($source)) { //用户数组 $sid = $source['uid']; } else { $sid = $source; } if (is_array($target)) { //目标用户数组 $tid = $target['uid']; } else { $tid = $target; } if (s_bad_id($sid) || s_bad_id($tid)) { return false; } $data = array(); $data['source_id'] = $sid; $data['target_id'] = $tid; $key = "user_friendship_by_tuid#{$tid}_{$sid}"; if (false === ($ret = s_memcache($key))) { if (false === ($ret = s_weibo_http("https://api.weibo.com/2/friendships/show.json", $data))) { return false; } //缓存中存储30秒 s_memcache($key, $ret, 30); } return $ret; }
function s_user_friends($uid, $page = 1, $size = 20, $sort = 0) { if (s_bad_id($uid) || s_bad_id($page) || s_bad_id($size) || s_bad_0id($sort)) { return false; } $data = array(); $data['uid'] = $uid; $data['page'] = $page; $data['count'] = $size; $data['sort'] = $sort; $key = "user_friends_by_uid#{$uid}_{$type}_{$sort}_{$page}_{$size}"; if (false === ($ret = s_memcache($key))) { if (false === ($ret = s_weibo_http("https://api.weibo.com/2/friendships/friends/bilateral.json", $data))) { return false; } //缓存中存储起来 s_memcache($key, $ret); } return $ret; }
function s_weibo_topic($key, $page = 1, $size = 10) { if (s_bad_string($key)) { return s_err_arg(); } //看cache中是否存在 $mkey = "weibo_search#" . $uid . $q . $page . $size . $istag . $sort . $start . $end . $sid; if (false === ($data = s_memcache($mkey))) { //缓存中没有,请求服务器 $params = array('sid' => $sid, 'page' => $page, 'count' => $size); if (is_string($q)) { $params['q'] = $q; } if (!s_bad_0id($istag)) { $params['istag'] = $istag; } if (!s_bad_id($uid)) { $params['uid'] = $uid; } if (!s_bad_id($start)) { $params['starttime'] = $start; } if (!s_bad_id($end)) { $params['endtime'] = $end; } if (false === ($data = s_weibo_http('http://i2.api.weibo.com/2/search/statuses.json', $params))) { return false; } //缓存起来60秒 s_memcache($mkey, $data, 60); } return $data; }