QQ Account logon and api operations, using oauth 2.0
The official SDKs are too large, which is simplified by myself. they provide simple Account logon, personal information retrieval, and sharing functions, if you need other functions, you can add them according to the official api documentation.
Qq. php
appid=$appid; $this->appkey=$appkey; $this->access_token=$access_token; } function login_url($callback_url, $scope=''){ $params=array( 'client_id'=>$this->appid, 'redirect_uri'=>$callback_url, 'response_type'=>'code', 'scope'=>$scope ); return 'https://graph.qq.com/oauth2.0/authorize?'.http_build_query($params); } function access_token($callback_url, $code){ $params=array( 'grant_type'=>'authorization_code', 'client_id'=>$this->appid, 'client_secret'=>$this->appkey, 'code'=>$code, 'state'=>'', 'redirect_uri'=>$callback_url ); $url='https://graph.qq.com/oauth2.0/token?'.http_build_query($params); $result_str=$this->http($url); $json_r=array(); if($result_str!='')parse_str($result_str, $json_r); return $json_r; } /** function access_token_refresh($refresh_token){ } **/ function get_openid(){ $params=array( 'access_token'=>$this->access_token ); $url='https://graph.qq.com/oauth2.0/me?'.http_build_query($params); $result_str=$this->http($url); $json_r=array(); if($result_str!=''){ preg_match('/callback\(\s+(.*?)\s+\)/i', $result_str, $result_a); $json_r=json_decode($result_a[1], true); } return $json_r; } function get_user_info($openid){ $params=array( 'openid'=>$openid ); $url='https://graph.qq.com/user/get_user_info'; return $this->api($url, $params); } function add_share($openid, $title, $url, $site, $fromurl, $images='', $summary=''){ $params=array( 'openid'=>$openid, 'title'=>$title, 'url'=>$url, 'site'=>$site, 'fromurl'=>$fromurl, 'images'=>$images, 'summary'=>$summary ); $url='https://graph.qq.com/share/add_share'; return $this->api($url, $params, 'POST'); } function api($url, $params, $method='GET'){ $params['access_token']=$this->access_token; $params['oauth_consumer_key']=$this->appid; $params['format']='json'; if($method=='GET'){ $result_str=$this->http($url.'?'.http_build_query($params)); }else{ $result_str=$this->http($url, http_build_query($params), 'POST'); } $result=array(); if($result_str!='')$result=json_decode($result_str, true); return $result; } function http($url, $postfields='', $method='GET', $headers=array()){ $ci=curl_init(); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ci, CURLOPT_TIMEOUT, 30); if($method=='POST'){ curl_setopt($ci, CURLOPT_POST, TRUE); if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); } $headers[]="User-Agent: qqPHP(piscdong.com)"; curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_URL, $url); $response=curl_exec($ci); curl_close($ci); return $response; }}
Config. php
3. index. php
Get_openid (); $ openid = $ qq_oid ['openid']; // Obtain the login user's open id // Obtain the login user information $ result = $ qq-> get_user_info ($ openid); var_dump ($ result ); /** // publish and share $ title = 'Open Source China'; // share the page title $ url = 'http: // www.oschina.net/'; // share the page url $ site = ''; // QQ application name $ fromurl = ''; // QQ application url $ result = $ qq-> add_share ($ openid, $ title, $ url, $ site, $ fromurl); var_dump ($ result); **/} else {// Generate a logon link $ qq = new qqPHP ($ qq_k, $ qq_s ); $ login_url = $ qq-> login_url ($ c Allback_url, $ scope); echo 'click to enter the authorization page';}?>
Callback. php
Access_token ($ callback_url, $ _ GET ['code']);} if (isset ($ result ['Access _ token']) & $ result ['Access _ token']! = '') {Echo 'authorization completed. please record
Access token:'; // Save the logon information. In this example, use session to save $ _ SESSION ['qq _ t'] = $ result ['Access _ token']; // access token} else {echo 'authorization failed';} echo'
Return ';?>