zoukankan      html  css  js  c++  java
  • 微信JSSDK开发

    开发者手册:

    https://mp.weixin.qq.com/cgi-bin/frame?t=advanced/dev_tools_frame&nav=10049&token=1085407263&lang=zh_CN

    学习地址:

    http://www.houdunren.com/houdunren18_lesson_230?vid=11410

     

    JSSDK使用步骤:

    按步骤配置一步一步操作:

    步骤一:绑定域名

    先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。

    备注:登录后可在“开发者中心”查看对应的接口权限。

    https://mp.weixin.qq.com/wiki?action=doc&id=mp1421141115&t=0.5922987409976531#2

    按上面的1.2.3.4步骤,把.txt文件下载到自己的服务器上,配置好域名,并能正常访问.

    phpstrom配置ftp自动上传:

    步骤二:引入JS文件

    在需要调用JS接口的页面引入如下JS文件,(支持https):http://res.wx.qq.com/open/js/jweixin-1.2.0.js

    备注:支持使用 AMD/CMD 标准模块加载方法加载

    步骤三:通过config接口注入权限验证配置

    所有需要使用JS-SDK的页面必须先注入配置信息,否则将无法调用(同一个url仅需调用一次,对于变化url的SPA的web app可在每次url变化时进行调用,目前Android微信客户端不支持pushState的H5新特性,所以使用pushState来实现web app的页面会导致签名失败,此问题会在Android6.2中修复)。

    1 wx.config({
    2     debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
    3     appId: '', // 必填,公众号的唯一标识
    4     timestamp: , // 必填,生成签名的时间戳
    5     nonceStr: '', // 必填,生成签名的随机串
    6     signature: '',// 必填,签名
    7     jsApiList: [] // 必填,需要使用的JS接口列表
    8 });

      1 <?php
      2 
      3 class Wx{
      4     protected $appid = 'wxc47243ed572e273d';
      5 
      6     protected $secret = '868e87533b1acda2ceb655ea94449ac0';
      7 
      8     /**
      9      * 获取access_token方法
     10      */
     11     public function getAccessToken(){
     12         //定义文件名称
     13         $name = 'token_' . md5($this->appid . $this->secret);
     14         //定义存储文件路径
     15         $filename = __DIR__ . '/cache/' . $name . '.php';
     16         //判断文件是否存在,如果存在,就取出文件中的数据值,如果不存在,就向微信端请求
     17         if (is_file($filename) && filemtime($filename) + 7100 > time()){
     18             $result = include $filename;
     19             //定义需要返回的内容$data
     20             $data = $result['access_token'];
     21         }else{
     22             //        https请求方式: GET
     23 //        https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
     24             //调用curl方法完成请求
     25             $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret=' . $this->secret;
     26             $result = $this->curl($url);
     27             //将返回得到的json数据转成php数组
     28             $result = json_decode($result,true);
     29             //将内容写入文件中
     30             file_put_contents($filename,"<?php
    return " . var_export($result,true) . ";
    ?>");
     31             //定义需要返回的内容
     32             $data = $result['access_token'];
     33         }
     34 
     35         //将得到的access_token的值返回
     36         return $data;
     37 
     38     }
     39 
     40     /**
     41      *
     42      * 获取临时票据方法
     43      *
     44      * @return mixed
     45      */
     46     public function getJsapiTicket(){
     47         //存入文件中,定义文件的名称和路径
     48         $name = 'ticket_' . md5($this->appid . $this->secret);
     49         //定义存储文件路径
     50         $filename = __DIR__ . '/cache/' . $name . '.php';
     51         //判断是否存在临时票据的文件,如果存在,就直接取值,如果不存在,就发送请求获取并保存
     52         if (is_file($filename) && filemtime($filename) + 7100 > time()){
     53             $result = include $filename;
     54         }else{
     55             //定义请求地址
     56             $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$this
     57                     ->getAccessToken().'&type=jsapi';
     58             //使用curl方法发送请求,获取临时票据
     59             $result = $this->curl($url);
     60             //转换成php数组
     61             $result = json_decode($result,true);
     62             //将获取到的值存入文件中
     63             file_put_contents($filename,"<?php
    return " . var_export($result,true) . ";
    ?>");
     64 
     65         }
     66         //定义返回的数据
     67         $data = $result['ticket'];
     68         //将得到的临时票据结果返回
     69         return $data;
     70     }
     71 
     72     /**
     73      * 获取签名方法
     74      */
     75     public function sign(){
     76         //需要定义4个参数,分别包括随机数,临时票据,时间戳和当前url地址
     77         $nonceStr = $this->makeStr();
     78         $ticket = $this->getJsapiTicket();
     79         $time = time();
     80         //组合url
     81         $url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
     82         //将4个参数放入一个数组中
     83         $arr = [
     84             'noncestr=' . $nonceStr,
     85             'jsapi_ticket=' . $ticket,
     86             'timestamp=' . $time,
     87             'url=' . $url,
     88         ];
     89         //对数组进行字段化排序
     90         sort($arr,SORT_STRING);
     91         //对数组进行组合成字符串
     92         $string = implode('&',$arr);
     93         //将字符串加密生成签名
     94         $sign = sha1($string);
     95         //由于调用签名方法的时候不只需要签名,还需要生成签名的时候的随机数,时间戳,所以我们应该返回由这些内容组成的一个数组
     96         $reArr = [
     97             'appId' => $this->appid,
     98             'timestamp' => $time,
     99             'nonceStr' => $nonceStr,
    100             'signature' => $sign,
    101             'url' => $url,
    102         ];
    103         //将数组返回
    104         return $reArr;
    105     }
    106 
    107     /**
    108      *
    109      * 生成随机数
    110      *
    111      * @return string
    112      */
    113     protected function makeStr(){
    114         //定义字符串组成的种子
    115         $seed = '1qaz2wsx3edc4rfv5tgb6yhn7ujm8ik9ol0p';
    116         //通过循环来组成一个16位的随机字符串
    117         //定义一个空字符串 用来接收组合成的字符串内容
    118         $str = '';
    119         for ($i = 0;$i < 16; $i++){
    120             //定义一个随机数
    121             $num = rand(0,strlen($seed) - 1);
    122             //循环连接随机生成的字符串
    123             $str .= $seed[$num];
    124         }
    125         //将随机数返回
    126         return $str;
    127     }
    128 
    129 
    130     /**
    131      *
    132      * 服务器之间请求的curl方法
    133      *
    134      * @param $url 请求地址
    135      * @param array $field post参数
    136      * @return string
    137      */
    138     public function curl($url,$field = []){
    139         //初始化curl
    140         $ch = curl_init();
    141         //设置请求的地址
    142         curl_setopt($ch,CURLOPT_URL,$url);
    143         //设置接收返回的数据,不直接展示在页面
    144         curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    145         //设置禁止证书校验
    146         curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    147         curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
    148         //判断是否为post请求方式,如果传递了第二个参数,就代表是post请求,如果么有传递,第二个参数为空,就是get请求
    149         if (!empty($field)){
    150             //设置请求超时时间
    151             curl_setopt($ch,CURLOPT_TIMEOUT,30);
    152             //设置开启post
    153             curl_setopt($ch,CURLOPT_POST,1);
    154             //传递post数据
    155             curl_setopt($ch,CURLOPT_POSTFIELDS,$field);
    156         }
    157         //定义一个空字符串,用来接收请求的结果
    158         $data = '';
    159         if (curl_exec($ch)){
    160             $data = curl_multi_getcontent($ch);
    161         }
    162         //关闭curl
    163         curl_close($ch);
    164         //将得到的结果返回
    165         return $data;
    166     }
    167 
    168 }
    169 //测试获取access_token值的方法
    170 //$obj = new Wx();
    171 //$data = $obj->getAccessToken();
    172 //echo $data;
    173 
    174 //测试获取jsapiticket方法
    175 //$obj = new Wx();
    176 //$data = $obj->getJsapiTicket();
    177 //echo $data;
    178 
    179 //测试生成签名方法
    180 //$obj = new Wx();
    181 //$data = $obj->sign();
    182 //echo '<pre>';
    183 //print_r($data);
    184 
    185 
    186 
    187 ?>
    [Haima的博客] http://www.cnblogs.com/haima/
  • 相关阅读:
    Uploader 文件上传
    filters过滤器的使用
    Calendar中遇到的问题
    中科院之旅
    Python基础教程:列表推导式详解
    不会也要知道的,Python四种实现排序的方法
    2021字节跳动校招秋招算法面试真题解题报告--leetcode19 删除链表的倒数第 n 个结点,内含7种语言答案
    2021字节跳动校招秋招算法面试真题解题报告--leetcode206 反转链表,内含7种语言答案
    求协方差
    国外卡组织的 交换费-interchangefee(发卡行服务费) 和 银联对比
  • 原文地址:https://www.cnblogs.com/haima/p/9363966.html
Copyright © 2011-2022 走看看