PHP教程

PHP-申请微信测试号开发微信公众号授权登录(一):拿code

本文主要是介绍PHP-申请微信测试号开发微信公众号授权登录(一):拿code,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

微信公众平台接口测试帐号申请

扫码登录后填写域名和token,无需公众帐号验证完毕后:快速申请接口测试号直接体验和测试公众平台所有高级接口

https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

请把以下代码保存成php文件,然后在接口配置信息中的URL填写此文件请求的网址

然后文件中的Token值修改成一致即可(Token值自己定义).

<?php

/**
 * wechat php test
 */

//define your token
define("TOKEN", "你设置的Token值");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();


class wechatCallbackapiTest
{
	public function valid()
  {
    $echoStr = $_GET["echostr"];


    //valid signature , option
    if($this->checkSignature()){
    	echo $echoStr;
    	exit;
    }
  }


  public function responseMsg()
  {
		//get post data, May be due to the different environments
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];


   	//extract post data
		if (!empty($postStr)){
       
       	$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $keyword = trim($postObj->Content);
        $time = time();
        $textTpl = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							<FuncFlag>0</FuncFlag>
							</xml>";      
				if(!empty( $keyword ))
        {
       		$msgType = "text";
        	$contentStr = "Welcome to wechat world!";
        	$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
        	echo $resultStr;
        }else{
        	echo "Input something...";
        }


    }else {
    	echo "";
    	exit;
    }
  }
		
	private function checkSignature()
	{
    $signature = $_GET["signature"];
    $timestamp = $_GET["timestamp"];
    $nonce = $_GET["nonce"];	
    		
		$token = TOKEN;
		$tmpArr = array($token, $timestamp, $nonce);
		sort($tmpArr);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}
}

?>

 

完成之后如下:然后扫码关注一下测试公众号

 

然后找到网页授权的接口:

本地获取code写的是Ip,不要写http,否则回调的时候时候会报redirect_uri 参数错误!

 

获取code的uniapp代码如下:你需要在微信开发者工具中打开页面

<template>
	<view class="springboard">
		<button @click="login()"
			type="default">登录获取code</button>
		<!-- <view class="">
			code:{{res}}
		</view>
		<view class="">
			access_token接口:{{result}}
		</view>
		<view class="">
			access_token :{{access_token}}
			<view class="">
				openid:{{openid}}
			</view>
		</view>
		<view class="">
			用户信息 {{userInfo}}
		</view> -->
		<!-- 跳板页面   这个界面主要用来 获取缓存的信息 如果没有缓存的信息则跳转去授权 如果有直接跳转去商城界面-->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				src: '',
				result: '',
				option: '',
				res: '',
				userInfo: '',
				access_token: '',
				openid: '',
				index: 0
			};
		},
		methods: {
			login() {
				this.getpower()
			},

			getpower() {
				const redirectURL = encodeURIComponent('http://192.168.1.9:8080/');//当前页面地址
				console.log(redirectURL)
					//获取地址
				const base = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=";
				let state = "STATE";
				// let surl;
				// let reserveUrl;
				let appid = 'wxcfebf476b4b7b439';//测试的appID,换成你的
				let scope = "snsapi_userinfo";
				window.location.href =
					base +
					appid +
					"&redirect_uri=" +
					redirectURL +
					"&response_type=code&scope=" +
					scope +
					"&state=" +
					state +
					"#wechat_redirect";
			}
		},
		onLoad() {

		}

	}
</script>

<style lang="less"
	scoped>
	.springboard {
		height: 99vh;
		position: relative;
		text-align: center;
		background-size: cover;
		// background-image: url('http://zons.oss-cn-shenzhen.aliyuncs.com/upload/20200703/20200703/1593768723d3eb9a9233e52948740d7eb8c3062d14.jpg');
		// background-image: url('http://zons.oss-cn-shenzhen.aliyuncs.com/upload/20200703/20200703/1593768995d3eb9a9233e52948740d7eb8c3062d14.jpg');
	}

	.LoginBtn {
		width: 50%;
		border: 1px solid #409eff;
		padding: 20rpx;
		display: inline-block;
		background-color: #409eff;
		color: #fff;
		border-radius: 10rpx;
	}
</style>

点击《登录获取code》 以后,code会出现在页面的url中,code=的值就是

拿到code以后,就要发送给后台服务端,服务端再请求微信接口换取用户信息:

未完待续······

 

这篇关于PHP-申请微信测试号开发微信公众号授权登录(一):拿code的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!