thinkphp5获取微信签名代码

2019-09-22

470

前面的代码是介绍微信分享,这篇是介绍获取微信签名的方法,代码完整可用,本人亲自测试过

thinkphp后端


<?php
namespace app\index\controller;
use think\Controller;
class Wanju extends Controller
{
    //微信配置
    public function get_sign(){
        // 微信 JS 接口签名校验工具: https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign
        $appid = '';
        $secret = '';
        // 获取token
        $token_data = file_get_contents(__DIR__.'/wechat_token.txt');
        $time = 7200;
        if (!empty($token_data)) {
            $token_data = json_decode($token_data, true);
            $time  = time() - $token_data['time'];
        }
        if ($time > 3600) {
            $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
            $token_res = $this->https_request($token_url);
            $token_res = json_decode($token_res, true);
            $token = $token_res['access_token'];
            $data = array(
                'time' =>time(),
                'token' =>$token
            );
 
            $res = file_put_contents(__DIR__.'/wechat_token.txt', json_encode($data));
            if ($res) {
                echo '更新 token 成功';
            }
        } else {
            $token = $token_data['token'];
        }
        // 获取ticket
        $ticket_data = file_get_contents(__DIR__.'/wechat_ticket.txt');
        $time1 = 7200;
        if (!empty($ticket_data)) {
            $ticket_data = json_decode($ticket_data, true);
            $time1  = time() - $ticket_data['time'];
        }
        if ($time1 > 3600) {
            $ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$token}&type=jsapi";
            $ticket_res = $this->https_request($ticket_url);
            $ticket_res = json_decode($ticket_res, true);
            $ticket = $ticket_res['ticket'];
            $data = array(
                'time'    =>time(),
                'ticket'  =>$ticket
            );
            $res = file_put_contents(__DIR__.'/wechat_ticket.txt', json_encode($data));
            if ($res) {
                echo '更新 ticket 成功';
            }
        } else {
            $ticket = $ticket_data['ticket'];
        }
        // 进行sha1签名
        $timestamp = time();
        $nonceStr = $this->createNonceStr();
        // 注意 URL 建议动态获取(也可以写死).
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
      
       

        $url = ""; // 调用JSSDK的页面地址                                    url最好最好加上  /  比如http://www.xxxx.com/  我这不加就出错
        //$url = $_SERVER['HTTP_REFERER']; // 前后端分离的, 获取请求地址(此值不准确时可以通过其他方式解决)
        $str = "jsapi_ticket={$ticket}&noncestr={$nonceStr}&timestamp={$timestamp}&url={$url}";
        $sha_str = sha1($str);
        $signPackage = array(
            "appId" =>  $appid,
            "nonceStr" => $nonceStr,
            "timestamp" => $timestamp,
            "signature" => $sha_str,
        );
        return $signPackage;
    }
    //随机字符串
    public function createNonceStr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
    // 模拟 http 请求
    public function https_request($url, $data = null){
        // curl 初始化
        $curl = curl_init();
        // curl 设置
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        // 判断 $data get  or post
        if ( !empty($data) ) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        // 执行
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }


  
 

}


我微信开发亲自测试过的,没有任何问题,前端的话可能每个人需求不一样,就不贴出来



 

 

DLweb`build
度良网络科技有限公司为您建设最时尚前沿的网站以及高端网站设计

沪ICP备16017573号

Copyright©2018 度良 All Rights Reserved.

友情链接:

义乌网站建设