此類可以用來做什麼?
QQ用戶oauth驗證登陸
取得使用者資訊
新增分享
取得相簿清單 上傳相片
發表日誌
發表說說
發表微博
檢查是否為認證空間的粉絲
發送圖片訊息到微博
取得微博用戶資訊
取得使用者的聽眾清單
取得使用者的收聽清單
收聽騰訊微博上的使用者
取得財付通用戶的收貨地址
- /**
- *
- * QQ開放平台API
- * @author maojianlw@139.com
- * @since 2013-06-20
- * @link http://www.eaglephp.com
- *
- */
-
- 班級QQ
- {
- 私人= null;
-
- private $appkey = null;
-
- private $callbcak = null;
-
- private $APIMap = array();
-
- private $APIMap = array() key
-
- private $ates priv = 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_del_blog,add_weibo,check_page_fans,add_t,add_pic_t,del_get8,_delpost_list_list,_ ,get_支付寶位址' ;
-
-
-
- public function __construct($appid, $appkey, $callbcak)
- {
- $this->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('位置:'.self::AUTH_CODE_URL.'?'.http_build_query($keysArr));
- }
-
-
- /**
- *
- * 回呼函數回傳access_token和open_id
- *
- */
- public function callback()
- {
- $state = Session::get('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']);
-
- // 根據token請求取得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);
- 回傳陣列('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('標題', '內容', '格式' => 'json'),
- 'GET '
- ),
- 'add_album' =>; array(
- 'https://graph.qq.com/photo/add_album',
- array('專輯名稱', '# albumdesc ', '#priv', 'format' => 'json') ,
- '發佈'
- ),
- 'upload_pic' => array(
- 'https://graph.qq . com/photo/upload_pic',
- array('圖片', '#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('標題', 'url', '#comment','#summary','#images','格式' => 'json','#type','#playurl','#nswb','site','fromurl'),
- 'POST'
- ),
- 'check_page_fans' => 陣列(
- 'https://graph.qq.com/user/check_page_fans',
- array('page_id' => '314416946','format' => 'json')
- ) ,
- /* 部落格*/
-
- '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('內容', '圖片', '格式' => 'json', '#clientip', ' #經度', '#緯度', '#syncflag', '#相容falg'),
- '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, $匹配))
- {
- $k = str_replace($matchs[0], '', $k);
- $optionArgList[$matchs[1]][] = $k ;
- }
- }
- else $v = null;
- }
- if(!isset($params[$k]) || $params[$k] === '' )
- {
- if($v == $pre) 繼續;
- 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} 未傳遞值。") ;
- }
- }
- $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]).'] 必須有一個值。 '?'.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} 不存在。");
-
- //從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判斷apiapi是否成功呼叫
- if( $responseArr['ret'] == 0) return $responseArr;
- else throw_exception('QQ_API_'.$name.' [ret:'.$responseArr['ret'].'] [errcode:'.$ responseArr['errcode'].'] '.$responseArr['msg']);
- }
-
-
- }
-
-
-
- }
複製程式碼
|