第一步:认证工作 首先登陆QQ互联首页https://connect.qq.com/进行个人/企业认证。
认证通过之后创建应用:
这里主要用到应用的APP ID 和 APP Key 这两个参数。
还要填写回调地址 就是请求qq接口获取code参数回调给你的地址
$app_id = "";
$app_secret = "";
$my_url="";
session_start();
$code = $_REQUEST["code"];
if(empty($code))
{
$_SESSION['state'] = md5(uniqid(rand(), TRUE));
$dialog_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
. $_SESSION['state'];
header('Location:'.$dialog_url);
}
if($_REQUEST['state'] == $_SESSION['state'] || 1)
{
$token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret . "&code=" . $code;
$response = file_get_contents($token_url);
$params = array();
parse_str($response, $params);
$graph_url = "https://graph.qq.com/oauth2.0/me?access_token=".$params['access_token'];
$str = file_get_contents($graph_url);
$user = json_decode($str);
if (isset($user->error))
{
echo "<h3>error:</h3>" . $user->error;
echo "<h3>msg :</h3>" . $user->error_description;
exit;
}
$user_data_url = "https://graph.qq.com/user/get_user_info?access_token={$params['access_token']}&oauth_consumer_key={$app_id}&openid={$user->openid}&format=json";
$user_data = file_get_contents($user_data_url);
$user_data = json_decode($user_data, true);
}else {
echo("The state does not match. You may be a victim of CSRF.");
}
|