header("Content-type: text/html; charset=utf-8");
$code = $_GET["code"];
/企业微信id/
define('corpid','wx*************');
/企业微信应用密匙/
define('corpsecret','MP7o-*******************************-tkoTI');
/获取access_token/
$access_token_str = file_get_contents("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=".corpid."&corpsecret=".corpsecret);
$access_token_list = json_decode($access_token_str,true);
$access_token = $access_token_list["access_token"];
/获取user_ticket/
$userinfo_str = file_get_contents("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=".$access_token."&code=".$code);
$userinfo_list = json_decode($userinfo_str,true);
$user_ticket = $userinfo_list["user_ticket"];
/携带user_ticket参数向微信服务器发送post请求,获取此用户信息/
/构造请求头包体/
$customMessageSendUrl = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail?access_token='.$access_token;
$postDataArr = array(
'user_ticket'=>$user_ticket,
);
$postJosnData = json_encode($postDataArr);
$ch = curl_init($customMessageSendUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postJosnData);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$user_str = curl_exec($ch);
$user_list = json_decode($user_str,true);
$userid = $user_list["userid"];//用户在企业微信id
$name = $user_list["name"];//用户姓名
$avatar = $user_list["avatar"]; //用户头像
/携带企业微信消息构造参数向微信服务器发送post请求,完成消息推送/
/构造请求头包体/
$customMessageSendUrl = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='.$access_token;
$postJosnData = '{
"touser" : "'.$userid.'",
"msgtype" : "text",
"agentid" : 1000025,
"text" : {
"content" : "企业消息通知"
}
}';
$ch = curl_init($customMessageSendUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postJosnData);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_exec($ch);
?>