#1 fosf
/*QQConnect.php放入Speedphp的扩展文件夹中去
* QQ互联 大万 2014-8-28
*/
class QQConnect
{
var $conifg = array();
const GET_AUTH_CODE_URL = "https://graph.qq.com/oauth2.0/authorize";
const GET_ACCESS_TOKEN_URL = "https://graph.qq.com/oauth2.0/token";
const GET_OPENID_URL = "https://graph.qq.com/oauth2.0/me";
const GET_USER_INFO_URL = "https://graph.qq.com/user/get_user_info";
const GET_ADD_T_URL = "https://graph.qq.com/t/add_t";
const GET_ADD_PIC_T_URL = "https://graph.qq.com/t/add_pic_t";
function __construct()
{
$params = spExt('QC');
$this->conifg['appid'] = $params['appid']; //填写APP ID
$this->conifg['appkey'] = $params['appkey']; //填写你APP KEY
$this->conifg['scope'] = $params['scope']; //'get_user_info';//API,根据你的需要
$this->conifg['callback'] = $params['callback']; //回调地址,例如:fosf.net
}
/**
* QQ互联登入
*/
function login()
{
$appid = $this->conifg['appid'];
$scope = $this->conifg['scope'];
$callback = $this->conifg['callback'];
$_SESSION['state'] = md5(uniqid(rand(), true));
$login_url = self::GET_AUTH_CODE_URL."?response_type=code&client_id=$appid&redirect_uri=" . urlencode($callback) . "&state=$_SESSION[state]&scope=$scope";
header("Location:$login_url");
}
/**
* 获取access_token
*/
function callback($code,$state)
{
$appid = $this->conifg['appid'];
$appkey = $this->conifg['appkey'];
$callback = $this->conifg['callback'];
//--------验证state防止CSRF攻击
if($_SESSION['state'] != $state){
$this->error->showError("30001");
$error = "error:
30001";
die($error);
}
//------构造请求access_token的url
$token_url = self::GET_ACCESS_TOKEN_URL."?grant_type=authorization_code&client_id=$appid&redirect_uri=" . urlencode($callback) ."&client_secret=$appkey&code=$code";
$response = $this->get_contents($token_url);
//$response = spClass('Http')->doGet($token_url);
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)) {
$error = "error:
" . $msg->error;
$error .= "msg :
" . $msg->error_description;
die($error);
}
}
$params = array();
parse_str($response, $params);
$_SESSION["access_token"] = $params["access_token"];
return $params["access_token"];
}
/**
* 获取 Open id
*/
function get_openid()
{
$graph_url = self::GET_OPENID_URL."?access_token=" . $_SESSION['access_token'];
$str = $this->get_contents($graph_url);
if (strpos($str, "callback") !== false) {
$lpos = strpos($str, "(");
$rpos = strrpos($str, ")");
$str = substr($str, $lpos + 1, $rpos - $lpos - 1);
}
$user = json_decode($str);
if (isset($user->error)) {
$error = '错误代码:' . $user->error . '
';
$error .= '提示:' . $user->error_description . '
';
die($error);
}
$_SESSION["openid"] = $user->openid;
return $user->openid;
}
/**
* 获取登录用户的昵称、头像、性别
* @param string $access_token 请求的token
* @param string $open_id 请求的openid
* @return $str 请求返回的内容
*/
function get_user_info($access_token, $open_id)
{
$appid = $this->conifg['appid'];
$access_token = empty($access_token)?$_SESSION["access_token"]:$access_token;
$open_id = empty($open_id)?$_SESSION["openid"]:$open_id;
$url = self::GET_USER_INFO_URL. "?oauth_consumer_key=$appid&access_token=$access_token&openid=$open_id&format=json";
$str = $this->get_contents($url);
$user_info = json_decode($str);
if ($user_info->ret !== 0) {
$error = '错误代码:' . $user_info->ret . '
';
$error .= '提示:' . $user_info->msg . '
';
die($error);
}
return $user_info;
}
/**
* 发普通文字微博
* @param string $content 请求的的文本
* @param string $access_token 请求的token
* @param string $open_id 请求的openid
* @return $str 请求返回的内容
*/
function add_t($content,$access_token, $open_id)
{
$url=self::GET_ADD_T_URL;
$appid = $this->conifg['appid'];
$access_token = empty($access_token)?$_SESSION["access_token"]:$access_token;
$open_id = empty($open_id)?$_SESSION["openid"]:$open_id;
$data = array(
'access_token'=>$access_token,
'oauth_consumer_key'=>$appid,
'openid'=>$open_id,
'format'=>'json',
'clientip'=>'59.52.57.188',
'content'=>$content
);
$result=$this->post_contents($url,$data);
return $response;
}
/**
* 发图文微博
* @param array $data 请求的数组
* @param string $access_token 请求的token
* @param string $open_id 请求的openid
* @return string 请求返回的内容
*/
function add_pic_t($data,$access_token, $open_id)
{
print_r($data);
$url=self::GET_ADD_PIC_T_URL;
$appid = $this->conifg['appid'];
$access_token = empty($access_token)?$_SESSION["access_token"]:$access_token;
$open_id = empty($open_id)?$_SESSION["openid"]:$open_id;
$tempdata = array(
'access_token'=>$access_token,
'oauth_consumer_key'=>$appid,
'openid'=>$open_id,
'format'=>'json',
'clientip'=>'59.52.57.188',
'content'=>$data['content'],
'pic'=> $data['pic']
);
$result=$this->post_contents($url,$tempdata);
return $response;
}
/**
* get_contents
* 服务器通过get请求获得内容
* @param string $url 请求的url,拼接后的
* @return string 请求返回的内容
*/
function get_contents($url){
$response='';
if (function_exists('file_get_contents')) $response = file_get_contents($url);
if (empty($response)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
}
//-------请求为空
if(empty($response)){
$error = '错误代码:50001
';
die($error);
}
return $response;
}
/**
* post_contents
* 服务器通过post请求获得内容
* @param string $url 请求的url,拼接后的
* @param array $data 请求的数组
* @return string 请求返回的内容
*/
function post_contents($url,$data){
$response='';
if (function_exists('file_get_contents')) $response = file_get_contents($url);
if (empty($response)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
}
//-------请求为空
if(empty($response)){
$error = '错误代码:50001
';
die($error);
}
return $response;
}
}
?>
配置 扩展里加入
'QC'=>array(
'appid'=>QQ_APPID, //appid QQ开放平台中去申请
'appkey'=>QQ_APPKEY,//appkey QQ开放平台中去申请
'scope'=>QQ_SCOPE,// get_user_info 需要的权限
'callback'=>QQ_CALLBACK,//回调地址
)
2014-08-28 21:51:18