此类可以用来做什么?
qq用户oauth验证登陆
获取用户信息
添加分享
获取相册列表
创建相册
上传相片
发表日志
发表说说
发表微博
检查是否是认证空间的粉丝
发图片消息到微博
获取微博用户信息
获取用户的听众列表
获取用户的收听列表
收听腾讯微博上的用户
获取财付通用户的收货地址 appid = $appid; $this->appkey = $appkey; $this->callbcak = $callbcak; } /** * 登录 */ public function login() { $state = md5(uniqid(rand(), true)); // 生成唯一随机串防csrf攻击 session::set('state', $state); $keysarr = array( 'response_type' => 'code', 'client_id' => $this->appid, 'redirect_uri' => $this->callbcak, 'scope' => self::scope, 'state' => $state ); header('location:'.self::auth_code_url.'?'.http_build_query($keysarr)); } /** * * 回调函数返回access_token和open_id * */ public function callback() { $state = session::get('state'); // 验证state防止csrf攻击 if(httprequest::getget('state') != $state) throw_exception('the state does not match. you may be a victim of csrf.'); session::set('state', null); $keysarr = array( 'grant_type' => 'authorization_code', 'client_id' => $this->appid, 'redirect_uri' => $this->callbcak, 'client_secret' => $this->appkey, 'code' => httprequest::getget('code') ); $response = curlrequest(self::access_token_url.'?'.http_build_query($keysarr)); if(strpos($response, 'callback') !== false) { $lpos = strpos($response, '('); $rpos = strrpos($response, ')'); $response = substr($response, $lpos + 1, $rpos - $lpos -1); $msg = json_decode($response); if(isset($msg->error)) throw_exception($msg->error.' '.$msg->error_description); } $params = array(); parse_str($response, $params); session::set('access_token', $params['access_token']); // 根据token请求获取openid $response = curlrequest(self::openid_url.'?access_token='.$params['access_token']); if(strpos($response, 'callback') !== false){ $lpos = strpos($response, '('); $rpos = strrpos($response, ')'); $response = substr($response, $lpos + 1, $rpos - $lpos -1); } $user = json_decode($response); if(isset($user->error)) throw_exception($msg->error.' '.$msg->error_description); session::set('open_id', $user->openid); return array('access_token' => $params['access_token'], 'open_id' => $user->openid); } /** * * 初始化数据 * */ protected function _initapi() { $this->keysarr = array( 'oauth_consumer_key' => (int)$this->appid, 'access_token' => session::get('access_token'), 'openid' => session::get('open_id') ); /** * * 初始化apimap * 加#表示非必须,无则不传入url(url中不会出现该参数), 'key' => 'val' 表示key如果没有定义则使用默认值val * 规则 array( baseurl, arglistarr, method) * * @var array */ $this->apimap = array( /* qzone */ 'add_blog' => array( 'https://graph.qq.com/blog/add_one_blog', array('title', 'format' => 'json', 'content' => null), 'post' ), 'add_topic' => array( 'https://graph.qq.com/shuoshuo/add_topic', array('richtype','richval','con','#lbs_nm','#lbs_x','#lbs_y','format' => 'json', '#third_source'), 'post' ), 'get_user_info' => array( 'https://graph.qq.com/user/get_user_info', array('format' => 'json'), 'get' ), 'add_one_blog' => array( 'https://graph.qq.com/blog/add_one_blog', array('title', 'content', 'format' => 'json'), 'get' ), 'add_album' => array( 'https://graph.qq.com/photo/add_album', array('albumname', '#albumdesc', '#priv', 'format' => 'json'), 'post' ), 'upload_pic' => array( 'https://graph.qq.com/photo/upload_pic', array('picture', '#photodesc', '#title', '#albumid', '#mobile', '#x', '#y', '#needfeed', '#successnum', '#picnum', 'format' => 'json'), 'post' ), 'list_album' => array( 'https://graph.qq.com/photo/list_album', array('format' => 'json') ), 'add_share' => array( 'https://graph.qq.com/share/add_share', array('title', 'url', '#comment','#summary','#images','format' => 'json','#type','#playurl','#nswb','site','fromurl'), 'post' ), 'check_page_fans' => array( 'https://graph.qq.com/user/check_page_fans', array('page_id' => '314416946','format' => 'json') ), /* wblog */ 'add_t' => array( 'https://graph.qq.com/t/add_t', array('format' => 'json', 'content','#clientip','#longitude','#compatibleflag'), 'post' ), 'add_pic_t' => array( 'https://graph.qq.com/t/add_pic_t', array('content', 'pic', 'format' => 'json', '#clientip', '#longitude', '#latitude', '#syncflag', '#compatiblefalg'), 'post' ), 'del_t' => array( 'https://graph.qq.com/t/del_t', array('id', 'format' => 'json'), 'post' ), 'get_repost_list' => array( 'https://graph.qq.com/t/get_repost_list', array('flag', 'rootid', 'pageflag', 'pagetime', 'reqnum', 'twitterid', 'format' => 'json') ), 'get_info' => array( 'https://graph.qq.com/user/get_info', array('format' => 'json') ), 'get_other_info' => array( 'https://graph.qq.com/user/get_other_info', array('format' => 'json', '#name', 'fopenid') ), 'get_fanslist' => array( 'https://graph.qq.com/relation/get_fanslist', array('format' => 'json', 'reqnum', 'startindex', '#mode', '#install', '#sex') ), 'get_idollist' => array( 'https://graph.qq.com/relation/get_idollist', array('format' => 'json', 'reqnum', 'startindex', '#mode', '#install') ), 'add_idol' => array( 'https://graph.qq.com/relation/add_idol', array('format' => 'json', '#name-1', '#fopenids-1'), 'post' ), 'del_idol' => array( 'https://graph.qq.com/relation/del_idol', array('format' => 'json', '#name-1', '#fopenid-1'), 'post' ), /* pay */ 'get_tenpay_addr' => array( 'https://graph.qq.com/cft_info/get_tenpay_addr', array('ver' => 1,'limit' => 5,'offset' => 0,'format' => 'json') ) ); } protected function _applyapi($baseurl, $argslist, $method) { $params = httprequest::getpost(); $optionarglist = ''; $keysarr = $this->keysarr; $pre = '#'; foreach ($argslist as $k=>$v) { if(!is_string($k)) { $k = $v; if(strpos($v, $pre) === 0) { $v = $pre; $k = substr($k, 1); if(preg_match('/-(\d$)/', $k, $matchs)) { $k = str_replace($matchs[0], '', $k); $optionarglist[$matchs[1]][] = $k; } } else $v = null; } if(!isset($params[$k]) || $params[$k] === '') { if($v == $pre) continue; elseif($v) $params[$k] = $v; else { if(isset($_files[$k]) && $_files[$k]['name']!='') { $uploaddir = getuploadaddr().'qq'.__ds__; mk_dir($uploaddir); $uploadobj = new upload(); $fileinfo = $uploadobj->uploadone($_files[$k], $uploaddir); $img = $uploaddir.$fileinfo[0]['savename']; $params[$k] = @{$img}; } else throw_exception(param {$k} not pass value.); } } $keysarr[$k] = $params[$k]; } // 检查选填参数必填一的情形 $i = 0; if(isset($optionarglist[1])) { foreach ($optionarglist[1] as $k=>$v) if(array_key_exists($v, $keysarr)) $i++; if(!$i) throw_exception('qq_api_param_error,['.implode(',', $optionarglist[1]).'] must hava one value.'); } $baseurl .= ($method == 'get') ? '?'.http_build_query($keysarr) : ''; $response = curlrequest($baseurl, $keysarr, $method); return json_decode($response, true); } public function __call($name, $args) { $this->_initapi(); if(!array_key_exists($name, $this->apimap)) throw_exception(qq_api_{$name} not exists.); //从apimap获取api相应参数 $baseurl = $this->apimap[$name][0]; $argslist = $this->apimap[$name][1]; $method = isset($this->apimap[$name][2]) ? $this->apimap[$name][2] : 'get'; $responsearr = $this->_applyapi($baseurl, $argslist, $method); //检查返回ret判断api是否成功调用 if($responsearr['ret'] == 0) return $responsearr; else throw_exception('qq_api_'.$name.' [ret:'.$responsearr['ret'].'] [errcode:'.$responsearr['errcode'].'] '.$responsearr['msg']); } }
复制代码