微信扫码关注公众号并登录PHP源码

微信扫码关注公众号并登录PHP源码

微信扫码关注公众号并登录PHP源码

《微信扫码关注公众号并登录PHP源码》可以用在任何网站上。只需要把“微信扫码关注公众号”功能与用户注册登录整合即可。微信扫码关注公众号并登录看起来容易,因为从界面上看,就两步:1、扫码;2、关注。但是微信接口很多坑不好绕过去。本套源码是全开源的,逻辑清晰,拿到即用。

极客网,已经完美整合《微信扫码关注公众号并登录PHP源码》,请退出登录后,再体验微信扫码关注公众号并登录。

部分代码如下:

<!--?php //配置文件 header("Content-Type: text/html; charset=utf-8"); session_start(); $host = "localhost"; $db_user = "aijikecom"; $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", "aijikecom"); //自己定义的token 就是个通信的私钥 $wechatObj = new wechatCallbackapiTest(); $wechatObj-&gt;valid();&lt;br ?--> class wechatCallbackapiTest {

public function valid() {
$echoStr = $_GET["echostr"];
if ($echoStr) {
if ($this-&gt;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-&gt;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-&gt;EventKey, true);
$openid = $postObj-&gt;FromUserName; //openid
$Event = strtolower($postObj-&gt;Event);
if ($Event == 'subscribe') {//首次关注
$is_first = 0;
//插入表
} elseif ($Event == 'unsubscribe' or $Event == 'scan') {//已关注
$is_first = 1;
}
$access_token = $this-&gt;getAccessToken();
$userinfo = $this-&gt;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-&gt;EventKey);
} else {
echo '咋不说哈呢';
exit;
}
}

private function getUserinfo($openid, $access_token) {
if ($access_token &amp;&amp; $openid) {
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&amp;openid=$openid&amp;lang=zh_CN";
$userinfo = $this-&gt;get_curl($url);
return $userinfo;
} else {
return array("code" =&gt; "userinfo_null");
}
}

public function getAccessToken() {
$appid = 'wx123456b0b6bbfcfc';
$secret = '12345657a3559cc5a5e606dc4735c47a';

$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=" . $appid . "&amp;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;
}
}

}
?&gt;
资源:200 学币
VIP优惠:免费
演示地址
0

评论0

请先 登录

没有账号? 忘记密码?