【微信开发】接口配置

由于近期刚开始学习微信开发,在网上查询整理了php与java两个版本的微信接口对接代码。

以下是php版本

define("TOKEN", "token");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
class wechatCallbackapiTest
{
    public function valid()
    {
        //随机字符串
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature()
    {
        //微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数
        $signature = $_GET["signature"];
        //时间戳
        $timestamp = $_GET["timestamp"];
        //随机数
        $nonce = $_GET["nonce"];
        $token =TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        //将token、timestamp、nonce三个参数进行字典排序
        sort($tmpArr);
        $tmpStr = implode( $tmpArr );
        //sha1加密
        $tmpStr = sha1( $tmpStr );
        //将加密后的字符串比较,验证接口
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}

java版(SSM框架)

@RequestMapping(method = RequestMethod.GET)
    public void get(HttpServletRequest request, HttpServletResponse response) {

        //微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数
        String signature = request.getParameter("signature");
        //时间戳
        String timestamp = request.getParameter("timestamp");
        //随机数
        String nonce = request.getParameter("nonce");
        //随机字符串
        String echostr = request.getParameter("echostr");

        PrintWriter out = null;
        try {

            out = response.getWriter();
            //验证签名
            if (SignUtil.checkSignture(signature, timestamp, nonce)) {
                out.print(echostr);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.close();
            out = null;
        }
    }

SignUtil.java

 public static boolean checkSignture(String signature, String timestamp, String nonce) {
        String[] arr = new String[]{Constant.TOKEN, timestamp, nonce};
        //将token、timestamp、nonce三个参数进行字典排序
        Arrays.sort(arr);
        StringBuilder conntent = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            conntent.append(arr[i]);
        }
        MessageDigest md = null;
        String tmpStr = null;

        try {
            md = MessageDigest.getInstance("SHA-1");
            byte[] digest = md.digest(conntent.toString().getBytes());
            tmpStr = byteToStr(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        conntent = null;
        //将sha1加密后的字符串与signature对比
        return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要开发ThinkPHP微信登录接口,你需要按照以下步骤进行操作: 1. 首先,你需要在微信开放平台上注册一个开发者账号,并创建一个新的应用程序。 2. 在ThinkPHP项目中,你需要安装EasyWeChat扩展包来实现微信登录接口的开发。可以使用Composer来安装,命令如下: ``` composer require overtrue/wechat ``` 3. 在ThinkPHP项目中,你需要创建一个控制器来处理微信登录请求,例如: ``` namespace app\controller; use think\Controller; use EasyWeChat\Factory; class WechatController extends Controller { public function login() { $config = [ 'app_id' => 'your-app-id', 'secret' => 'your-app-secret', 'oauth' => [ 'scopes' => ['snsapi_userinfo'], 'callback' => '/wechat/callback', ], ]; $app = Factory::officialAccount($config); $response = $app->oauth->redirect(); return $response; } public function callback() { $config = [ 'app_id' => 'your-app-id', 'secret' => 'your-app-secret', ]; $app = Factory::officialAccount($config); $user = $app->oauth->user(); // 保存用户信息到数据库等操作 return redirect('/user/profile'); } } ``` 在上面的代码中,`login()`方法用于重定向到微信授权页面,`callback()`方法用于处理授权回调并获取用户信息。 4. 在ThinkPHP项目中,你需要创建一个路由来指定微信登录请求的URL,例如: ``` Route::get('wechat/login', 'WechatController@login'); Route::get('wechat/callback', 'WechatController@callback'); ``` 5. 最后,你需要在微信开放平台上配置授权回调URL,例如: ``` http://your-domain.com/wechat/callback ``` 这样,你就可以在ThinkPHP项目中实现微信登录接口了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值