PHP - 钉钉 - 扫码 - 获取信息
引入 js
<script type="text/javascript" src="//g.alicdn.com/dingding/open-develop/0.8.4/dingtalk.js"></script>
内部代码如下,作用是实现二维码的展现。
<script>
!function (window, document) {
function d(a) {
var e, c = document.createElement("iframe"),
d = "https://login.dingtalk.com/login/qrcode.htm?goto=" + a.goto ;
//console.log(d);
d += a.style ? "&style=" + encodeURIComponent(a.style) : "",
d += a.href ? "&href=" + a.href : "",
c.src = d,
c.frameBorder = "0",
c.allowTransparency = "true",
c.scrolling = "no",
c.width = a.width ? a.width + 'px' : "365px",
c.height = a.height ? a.height + 'px' : "400px",
e = document.getElementById(a.id),
e.innerHTML = "",
e.appendChild(c)
}
window.DDLogin = d
}(window, document);
</script>
实例化 js 对象
<div id="login_container"></div>
var obj = DDLogin({
//<div id="login_container"></div>
id:"login_container",
goto: "<?php echo $goto;?>",
style: "",
width : "",
height: ""
});
div 最好放到实例化 js 之前。
goto 参数的构造:
$goto = urlencode("https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=".$appid."&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=".$redirect_url);
appid 和 appSecret 去后台自己申请,redirect_url 为扫码后的跳转地址,goto 使用前需 urlencode();
loginTmpCode
var hanndleMessage = function (event) {
//拿到loginTmpCode后就可以在这里构造跳转链接进行跳转了
var loginTmpCode = event.data;
var origin = event.origin;
var gotourl = "https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=<?php echo $appid;?>&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=<?php echo $redirect_url;?>&loginTmpCode="+loginTmpCode;
//console.log(gotourl);
window.location.href = gotourl;
};
if (typeof window.addEventListener != 'undefined') {
window.addEventListener('message', hanndleMessage, false);
} else if (typeof window.attachEvent != 'undefined') {
window.attachEvent('onmessage', hanndleMessage);
}
gotourl 为扫码后跳转地址,跳转此链接成功后便会重定向到 redirect_url,并 get 追加[临时授权码code] 和 state 参数。
redirect_url 页面
//根据APPID和APPSECRET获取的TOKEN
$access_token = Auth::getToken();
$access_token = json_decode(json_encode($access_token), true);
$access_token = $access_token['access_token'];
//echo $access_token."</br>";
//临时授权码
$code = $_REQUEST['code'];
//echo $code."</br>";
//获取持久授权码
$url = "https://oapi.dingtalk.com/sns/get_persistent_code?access_token=".$access_token;
$params = array('tmp_auth_code' => $code);
//var_dump($params);
$method = "POST";
$pcode = Http::request($url, json_encode($params), $method);
$pcode = json_decode($pcode, true);
//var_dump($pcode);
//持久授权码
$persistent_code = $pcode['persistent_code'];
$openid = $pcode['openid'];
$unionid = $pcode['unionid'];
//echo $persistent_code."</br>".$openid."</br>".$unionid."</br>";
//获取SNS_TOKEN
$url = "https://oapi.dingtalk.com/sns/get_sns_token?access_token=".$access_token;
$params = array(
'openid' => $openid,
'persistent_code' => $persistent_code
);
$method = "POST";
$sns_token = Http::request($url, json_encode($params), $method);
//var_dump($sns_token);
$sns_token = json_decode($sns_token, true);
$sns_token = $sns_token['sns_token'];
//获取用户信息
$url = "https://oapi.dingtalk.com/sns/getuserinfo?sns_token=".$sns_token;
@$userinfo = Http::request($url);
$userinfo = json_decode($userinfo, true);
//var_dump($userinfo);
$dingId = $userinfo['user_info']['dingId'];
$nick = $userinfo['user_info']['nick'];
$unionid = $userinfo['user_info']['unionid'];
$openid = $userinfo['user_info']['openid'];
echo "dingId:".$dingId."</br>";
echo "nick:".$nick."</br>";
echo "unionid:".$unionid."</br>";
echo "openid:".$openid."</br>";
HTTP/HTTPS 请求方法
/**
* 发起一个HTTP/HTTPS的请求
* @param $url 接口的URL
* @param $params 接口参数 array('content'=>'test', 'format'=>'json');
* @param $method 请求类型 GET|POST
* @param $multi 图片信息
* @param $extheaders 扩展的包头信息
* @return string
*/
public static function request( $url , $params = array(), $method = 'GET' , $multi = false, $extheaders = array())
{
if(!function_exists('curl_init')) exit('Need to open the curl extension');
$method = strtoupper($method);
$ci = curl_init();
curl_setopt($ci, CURLOPT_USERAGENT, '');
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ci, CURLOPT_TIMEOUT, 30);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ci, CURLOPT_HEADER, false);
$headers = (array)$extheaders;
switch ($method)
{
case 'POST':
curl_setopt($ci, CURLOPT_POST, TRUE);
if (!empty($params))
{
if($multi)
{
foreach($multi as $key => $file)
{
$params[$key] = '@' . $file;
}
curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
$headers[] = 'Expect: ';
}
else
{
curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
}
}
break;
case 'DELETE':
case 'GET':
$method == 'DELETE' && curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
if (!empty($params))
{
$url = $url . (strpos($url, '?') ? '&' : '?')
. (is_array($params) ? http_build_query($params) : $params);
}
break;
}
curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );
curl_setopt($ci, CURLOPT_URL, $url);
curl_setopt($ci, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($params))
);
$response = curl_exec($ci);
curl_close ($ci);
return $response;
}
扫码页面源码
<head>
<title>扫码</title>
<!--扫码登录js-->
<!--script src="//g.alicdn.com/dingding/dinglogin/0.0.5/ddLogin.js"></script-->
<script>
!function (window, document) {
function d(a) {
var e, c = document.createElement("iframe"),
d = "https://login.dingtalk.com/login/qrcode.htm?goto=" + a.goto ;
//console.log(d);
d += a.style ? "&style=" + encodeURIComponent(a.style) : "",
d += a.href ? "&href=" + a.href : "",
c.src = d,
c.frameBorder = "0",
c.allowTransparency = "true",
c.scrolling = "no",
c.width = a.width ? a.width + 'px' : "365px",
c.height = a.height ? a.height + 'px' : "400px",
e = document.getElementById(a.id),
e.innerHTML = "",
e.appendChild(c)
}
window.DDLogin = d
}(window, document);
</script>
</head>
<body>
<div id="login_container"></div>
<?php
$appid = "appid";
$appSecret = "appsecret";
$redirect_url = "http://www.redirecturl.com";
$goto = urlencode("https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=".$appid."&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=".$redirect_url);
?>
<script>
var obj = DDLogin({
//<div id="login_container"></div>
id:"login_container",
goto: "<?php echo $goto;?>",
style: "",
width : "",
height: ""
});
var hanndleMessage = function (event) {
//拿到loginTmpCode后就可以在这里构造跳转链接进行跳转了
var loginTmpCode = event.data;
var origin = event.origin;
var gotourl = "https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=<?php echo $appid;?>&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=<?php echo $redirect_url;?>&loginTmpCode="+loginTmpCode;
//console.log(gotourl);
window.location.href = gotourl;
};
if (typeof window.addEventListener != 'undefined') {
window.addEventListener('message', hanndleMessage, false);
} else if (typeof window.attachEvent != 'undefined') {
window.attachEvent('onmessage', hanndleMessage);
}
</script>
</body>
Wow, that's what I was exploring for, what a stuff!existinghere at this web site, thanks admin of this wweb site.
You are welcome @(太开心)
这是用什么搭建的博客,还是劝自己写的
typecho
看着就不像妹子.......
主题很赞!!
谢谢夸奖 ๑乛◡乛๑
妹子,你怎么这么厉害,震惊了
低调 ~
And, I am a boy ~
感觉好厉害୧(๑•̀⌄•́๑)૭
官方文档好多坑,难得做了出来,赶紧记录一下 o(^▽^)o
@(真棒)
谢谢夸奖 ๑乛◡乛๑