Welcome

首页 / 网页编程 / PHP / PHP微信JSSDK签名示例

<?php

namespace weisoft;

use think\Db;

class JSSDK

{

    private $appId = '';

    private $appSecret = '';

    public function __construct($appId, $appSecret) {

        $this->appId = $appId;

        $this->appSecret = $appSecret;

    }

    function getSignPackage()

    {

        // 获取token

        $token_data = file_get_contents($_SERVER["DOCUMENT_ROOT"].'/uploads/wechat_token.txt');

        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=".$this->appId."&secret=".$this->appSecret;

            $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($_SERVER["DOCUMENT_ROOT"].'/uploads/wechat_token.txt', json_encode($data));

            if ($res) {

                echo '更新 token 成功';

            }

        } else {

            $token = $token_data['token'];

        }

        $ticket_data = file_get_contents($_SERVER["DOCUMENT_ROOT"].'/uploads/wechat_ticket.txt');

        if (!empty($ticket_data)) {

            $ticket_data = json_decode($ticket_data, true);

        }

        $time = time() - @$ticket_data['time'];

        if ($time > 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($_SERVER["DOCUMENT_ROOT"].'/uploads/wechat_ticket.txt', json_encode($data));

            if ($res) {

                echo '更新 ticket 成功';

            }

        } else {

            $ticket = $ticket_data['ticket'];

        }

        $timestamp = time();

        $nonceStr =$this->createNonceStr();

        $url = empty(input('post.url'))?"http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]":input('post.url','',null);

        $str = "jsapi_ticket={$ticket}&noncestr={$nonceStr}&timestamp={$timestamp}&url={$url}";

        $signature = sha1($str);

        $signPackage = array(

            "appId"     => $this->appId,

            "jsapi_ticket"=>$ticket,

            "nonceStr"  => $nonceStr,

            "timestamp" => $timestamp,

            "url"       => $url,

            "signature" => $signature

        );

        return json($signPackage);

    }

    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 请求

     * @param  String $url  请求网址

     * @param  Array  $data 数据

     */

    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;

    }

}

?>