《微信扫码关注公众号并登录PHP源码》可以用在任何网站上。只需要把“微信扫码关注公众号”功能与用户注册登录整合即可。微信扫码关注公众号并登录看起来容易,因为从界面上看,就两步:1、扫码;2、关注。但是微信接口很多坑不好绕过去。本套源码是全开源的,逻辑清晰,拿到即用。
尚学网,已经完美整合《微信扫码关注公众号并登录PHP源码》,请退出登录后,再体验微信扫码关注公众号并登录。
部分代码如下:
<!--?php //配置文件 header("Content-Type: text/html; charset=utf-8"); session_start(); $host = "localhost"; $db_user = "study5com"; $db_pass = "123456"; $db_name = "demo"; $timezone = "Asia/Shanghai"; $link = mysql_connect($host, $db_user, $db_pass); mysql_select_db($db_name, $link); mysql_query("SET names UTF8"); $appid = 'wx123456b0b6bbfcfc'; $appsecret = '12345657a3559cc5a5e606dc4735c47a'; ?-->
<!--?php header("Content-type:text/html;charset=utf-8"); define("TOKEN", "study5com"); //自己定义的token 就是个通信的私钥 $wechatObj = new wechatCallbackapiTest(); $wechatObj->valid();<br ?--> class wechatCallbackapiTest {
public function valid() {
$echoStr = $_GET["echostr"];
if ($echoStr) {
if ($this->checkSignature()) {
file_put_contents('access_token.txt', "jj." . date("Y-m-d H:i:s"));
echo $echoStr;
} else {
file_put_contents('access_token.txt', "dd." . date("Y-m-d H:i:s"));
}
} else {
$this->responseMsg();
exit;
}
}
public function responseMsg() {
file_put_contents('userinfo.txt', date("Y-m-d H:i:s"));
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)) {
include_once 'config.php';
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$scenes = json_decode($postObj->EventKey, true);
$openid = $postObj->FromUserName; //openid
$Event = strtolower($postObj->Event);
if ($Event == 'subscribe') {//首次关注
$is_first = 0;
//插入表
} elseif ($Event == 'unsubscribe' or $Event == 'scan') {//已关注
$is_first = 1;
}
$access_token = $this->getAccessToken();
$userinfo = $this->getUserinfo($openid, $access_token);
if($is_first == 0){ //首次关注
$qr_scene_str = json_decode($userinfo['qr_scene_str'],true);
$mtype = $qr_scene_str['mtype'];
$scene_id = $qr_scene_str['scene_id'];
file_put_contents('mtype_scene_id.txt', $mtype."***".$scene_id);
}else{
$scene_id = $scenes['scene_id'];
$mtype = $scenes['mtype'];
}
$sql = "UPDATE `qrcode` SET `openid` = '" . $openid . "',logintime='" . time() . "',is_first=" . $is_first . ",nickname='" . $userinfo['nickname'] . "'"
. ",avatar='" . $userinfo['headimgurl'] . "',sex='" . $userinfo['sex'] . "',province='" . $userinfo['province'] . "',city='" . $userinfo['city'] . "',country='" . $userinfo['country'] . "' WHERE `id` =" . $scene_id . "";
mysql_query($sql);
file_put_contents('userinfo.txt', 'userinfo: ' . json_encode($userinfo) . $sql . "|" . $postObj->EventKey);
} else {
echo '咋不说哈呢';
exit;
}
}
private function getUserinfo($openid, $access_token) {
if ($access_token && $openid) {
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
$userinfo = $this->get_curl($url);
return $userinfo;
} else {
return array("code" => "userinfo_null");
}
}
public function getAccessToken() {
$appid = 'wx123456b0b6bbfcfc';
$secret = '12345657a3559cc5a5e606dc4735c47a';
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret . "";
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$access_tokens = json_decode($result, true);
$access_token = $access_tokens['access_token'];
return $access_token;
}
public function get_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$rs = $result ? json_decode($result, true) : "";
return $rs;
}
private function checkSignature() {
$echoStr = $_GET["echostr"];
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if ($tmpStr == $signature) {
return true;
} else {
return false;
}
}
}
?>
public function valid() {
$echoStr = $_GET["echostr"];
if ($echoStr) {
if ($this->checkSignature()) {
file_put_contents('access_token.txt', "jj." . date("Y-m-d H:i:s"));
echo $echoStr;
} else {
file_put_contents('access_token.txt', "dd." . date("Y-m-d H:i:s"));
}
} else {
$this->responseMsg();
exit;
}
}
public function responseMsg() {
file_put_contents('userinfo.txt', date("Y-m-d H:i:s"));
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)) {
include_once 'config.php';
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$scenes = json_decode($postObj->EventKey, true);
$openid = $postObj->FromUserName; //openid
$Event = strtolower($postObj->Event);
if ($Event == 'subscribe') {//首次关注
$is_first = 0;
//插入表
} elseif ($Event == 'unsubscribe' or $Event == 'scan') {//已关注
$is_first = 1;
}
$access_token = $this->getAccessToken();
$userinfo = $this->getUserinfo($openid, $access_token);
if($is_first == 0){ //首次关注
$qr_scene_str = json_decode($userinfo['qr_scene_str'],true);
$mtype = $qr_scene_str['mtype'];
$scene_id = $qr_scene_str['scene_id'];
file_put_contents('mtype_scene_id.txt', $mtype."***".$scene_id);
}else{
$scene_id = $scenes['scene_id'];
$mtype = $scenes['mtype'];
}
$sql = "UPDATE `qrcode` SET `openid` = '" . $openid . "',logintime='" . time() . "',is_first=" . $is_first . ",nickname='" . $userinfo['nickname'] . "'"
. ",avatar='" . $userinfo['headimgurl'] . "',sex='" . $userinfo['sex'] . "',province='" . $userinfo['province'] . "',city='" . $userinfo['city'] . "',country='" . $userinfo['country'] . "' WHERE `id` =" . $scene_id . "";
mysql_query($sql);
file_put_contents('userinfo.txt', 'userinfo: ' . json_encode($userinfo) . $sql . "|" . $postObj->EventKey);
} else {
echo '咋不说哈呢';
exit;
}
}
private function getUserinfo($openid, $access_token) {
if ($access_token && $openid) {
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
$userinfo = $this->get_curl($url);
return $userinfo;
} else {
return array("code" => "userinfo_null");
}
}
public function getAccessToken() {
$appid = 'wx123456b0b6bbfcfc';
$secret = '12345657a3559cc5a5e606dc4735c47a';
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret . "";
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$access_tokens = json_decode($result, true);
$access_token = $access_tokens['access_token'];
return $access_token;
}
public function get_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$rs = $result ? json_decode($result, true) : "";
return $rs;
}
private function checkSignature() {
$echoStr = $_GET["echostr"];
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if ($tmpStr == $signature) {
return true;
} else {
return false;
}
}
}
?>
请先 登录 !