? ??? ??? ??? ? ????
QQ ??? o?? ???
??? ?? ????
?? ??
?? ?? ????
?? ?? ???
?? ???
?? ???
?? ??
??? ???
???? ??? ???? Weibo? ?? ??? ??
Weibo ??? ?? ????
??? ??? ?? ????
??? ?? ?? ????
Tencent ???? ? ?? ???
獲取財(cái)付? ????? ???? 收貨地址
- /**
- *
- * QQ ?? ??? API
- * @author maojianlw@139.com
- * @since 2013-06-20
- * @link http://www.eaglephp.com
- *
- */
- class QQ
- {
-
- ??? $ appid = null;
-
- private $appkey = null;
-
- private $callbcak = null;
-
- private $APIMap = array();
-
- private $ keyArr = array();
-
- const VERSION = '2.0';
-
- const AUTH_CODE_URL = 'https://graph.qq.com/oauth2.0/authorize';
-
- const ACCESS_TOKEN_URL = 'https://graph.qq.com/oauth2.0/token';
-
- const OPENID_URL = 'https://graph.qq.com/oauth2.0/me';
-
- const SCOPE = 'get_user_info,add_share,list_album,add_album,upload_pic,add_topic,add_one_blog,add_weibo,check_page_fans,add_t,add_pic_t,del_t,get_repost_list,get_info,get_other_info,get_fanslist,get_idolist,add_idol,del_idol,get_ten pay_addr' ;
-
-
-
- ?? ?? __construct($appid, $appkey, $callbcak)
- {
- $this->appid = $appid;
- $this-> ;appkey = $appkey;
- $this->callbcak = $callbcak;
- }
-
-
- /**
- * ???
- */
- ?? ?? ???()
- {
- $state = md5(uniqid(rand(), true)); // 生成唯一隨機(jī)串防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? ?????.
- *
- */
- ?? ?? ??()
- {
- $state = Session::get('state');
-
- // 驗(yàn)證state防止CSRF攻擊
- if (HttpRequest::getGet('state') != $state) throw_Exception('??? ???? ????. 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']);
-
- // 根據(jù)token請(qǐng)求獲取openid
- $response =curlRequest(self::OPENID_URL.'?access_token='.$params['access_token']);
-
- if(strpos($ ??, '??') !== 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);
- ?? ?? ('??? ??' => $params['access_token'], 'open_id' => $user->openid);
- }
-
-
- /**
- *
- * ??? ???
- *
- */
- ?? ?? _initAPI()
- {
- $this->keysArr = array(
- 'oauth_consumer_key' => (int)$this->appid,
- 'access_token' => ??::get('access_token'),
- 'openid' => ??::get('open_id')
- );
-
- /**
- *
- * APIMap ???
- * ???? ??? ????? #? ?????. ??? ??? URL? ???? ????(? ????? URL? ???? ????). 'key' => 'val' ?? ???? ?? ??? ?????. ?? ?? ???? ?????. val
- * rule 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' => ??(
- 'https://graph.qq.com/user/get_user_info',
- ??('??' => 'json'),
- 'GET'
- ),
- 'add_one_blog' => array(
- 'https://graph.qq.com/blog/add_one_blog',
- array('title', 'content', 'format' => 'json'),
- 'GET '
- ),
- '?? ??' => array(
- 'https://graph.qq.com/photo/add_album',
- array('?? ??', '#albumdesc', '#priv', 'format' => 'json') ,
- '???'
- ),
- 'upload_pic' => array(
- 'https://graph.qq.com/photo/upload_pic',
- array('picture', '#photodesc', '#title', '#albumid', '#mobile', '#x', '#y', '#needfeed', '#successnum', '#picnum', 'format' => 'json'),
- 'POST'
- ),
- '??_??' => array(
- 'https://graph.qq.com/photo/list_album',
- array('format' => 'json')
- ),
- 'add_share' => array(
- 'https://graph.qq.com/share/add_share',
- array('??', 'url', '#comment','#summary','#images',' ??' => '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', '#?????'),
- 'POST'
- ),
- 'add_pic_t' => array(
- 'https://graph.qq.com/t/add_pic_t',
- array('content', 'pic', 'format' => 'json', '#clientip', ' #??', '#??', '#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', '??' => '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', ' #??')
- ),
- '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'
- ),
- /* ?? */
-
- 'get_tenpay_addr' => array(
- 'https://graph.qq.com/cft_info/get_tenpay_addr',
- array('ver' => 1,'limit' => 5,'offset' => 0, 'format' => 'json')
- )
- );
- }
-
-
- ?? ?? _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 = ? Upload();
- $fileInfo = $uploadObj->uploadOne($_FILES[$k], $uploadDir);
- $img = $uploadDir.$ fileInfo[0]['savename'];
- $params[$k] = "@{$img}";
- }
- else throw_Exception("param {$k} ?? ???? ????.") ;
- }
- }
- $keysArr[$k] = $params[$k];
- }
-
- // 檢查選填參數(shù)必填一? ???
- $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]).'] ??? ?? ??? ???.');
- }
-
- $baseUrl .= ($method == 'GET') ? '?'.http_build_query($keysArr) : '';
- $response =curlRequest($baseUrl, $keysArr, $method);
- return json_decode($response, true);
- }
-
-
- ?? ?? __call($name, $args)
- {
- $this->_initAPI();
- if(!array_key_exists($name, $this->APIMap) ) throw_Exception("QQ_api_{$name}?(?) ???? ????.");
-
- //從APIMap獲取api?應(yīng)參數(shù)
- $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是否成功調(diào)用
- if($responseArr['ret'] == 0) return $responseArr;
- else throw_Exception('QQ_API_'.$name.' [ret:'.$ responseArr['ret'].'] [errcode:'.$responseArr['errcode'].'] '.$responseArr['msg']);
- }
-
-
- }
-
復(fù)代碼
|