$code = $_GET['code'];
$aa = 'https://oapi.dingtalk.com/sns/gettoken?appid=dingodz6n97wis&appsecret=pKy2MnFWMp4sPBTgwGOujWI6rT5QOxS_0pIKyZWREyTSaW2SmyziJgEQ8Dxs0';
$res = file_get_contents($aa);
$arr = json_decode($res,1);
$access_token = $arr['access_token'];
$u = 'https://oapi.dingtalk.com/sns/get_persistent_code?access_token='.$access_token;
$data = array('tmp_auth_code'=>$code);
$str = json_encode($data);
$res1 = getcodes($u,$str);
$arr1 = json_decode($res1,1);
$openid = $arr1['openid'];
$persistent_code = $arr1['persistent_code'];
$data = array('openid'=>$openid,'persistent_code'=>$persistent_code);
$str = json_encode($data);
$u = 'https://oapi.dingtalk.com/sns/get_sns_token?access_token='.$access_token;
$res2 = getcodes($u,$str);
$arr2 = json_decode($res2,1);
$sns_token = $arr2['sns_token'];
$u = 'https://oapi.dingtalk.com/sns/getuserinfo?sns_token='.$sns_token;
$res3 = file_get_contents($u);
echo '
';
print_r(json_decode($res3,1));
function getcodes($remote_server,$post_string){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remote_server);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json;charset=utf-8']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
注意:请求https连接,curl参数缺少导致请求失败,post请求必须加头信息说明数据格式,否则返回数据格式不正确