侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

php一个文件搞定微信jssdk配置

微信(小程序)  /  管理员 发布于 7年前   185

php一个文件搞定微信jssdk配置:

包括缓存,包括https通讯,获取微信access_token,签名什么的都有。但是防范性编程做得比较少,商业用的话,需要完善下代码。

使用姿势

^ajax(Common.ServerUrl + "GetWX.php", { data: {  Type: "config",  url: location.href.split('#')[0] }, dataType: 'json', type: 'get', timeout: 5000, success: function(data) {  wx.config({   debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。   appId: '……', // 必填,公众号的唯一标识   timestamp: data.timestamp, // 必填,生成签名的时间戳   nonceStr: data.nonceStr, // 必填,生成签名的随机串   signature: data.signature, // 必填,签名,见附录1   jsApiList: ["getLocation"] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2  }); }})wx.ready(function() { wx.getLocation({  type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'  success: function(res) {   var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90   var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。   plus2.storage.setItem("latitude", latitude);   plus2.storage.setItem("longitude", longitude);  } });});

服务端

GetWX.php

$jsapi_ticket, "nonceStr"=>$nonceStr,"timestamp"=>$timestamp,"url"=>$url,"signature"=>$signature);  echo json_encode($result); } function getSignature($jsapi_ticket,$noncestr, $timestamp, $url){  $string1 = "jsapi_ticket=".$jsapi_ticket."&noncestr=".$noncestr."×tamp=".$timestamp."&url=".$url;  $sha1 = sha1($string1);  return $sha1; } function getJsapi_ticket(){  $cache = new Cache();  $cache = new Cache(7000, 'cache/'); //需要创建cache文件夹存储缓存文件。  //从缓存从读取键值 $key 的数据  $jsapi_ticket = $cache -> get("jsapi_ticket");  $access_token = getAccess_token();  //如果没有缓存数据  if ($jsapi_ticket == false) {   $access_token = getAccess_token();   $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket';    $data = array('type'=>'jsapi','access_token'=>$access_token);    $header = array();    $response = json_decode(curl_https($url, $data, $header, 5));    $jsapi_ticket = $response->ticket;   //写入键值 $key 的数据   $cache -> put("jsapi_ticket", $jsapi_ticket);  }  return $jsapi_ticket; } function getAccess_token(){  $cache = new Cache();  $cache = new Cache(7000, 'cache/');  //从缓存从读取键值 $key 的数据  $access_token = $cache -> get("access_token");  //如果没有缓存数据  if ($access_token == false) {   $url = 'https://api.weixin.qq.com/cgi-bin/token';    $data = array('grant_type'=>'client_credential','appid'=>$APPID,'secret'=>$SECRET);    $header = array();   $response = json_decode(curl_https($url, $data, $header, 5));    $access_token = $response->access_token;   //写入键值 $key 的数据   $cache -> put("access_token", $access_token);  }  return $access_token; } /** curl 获取 https 请求  * @param String $url 请求的url  * @param Array $data 要l送的  * @param Array $header 请求时发送的header  * @param int $timeout 超时时间,默认30s  */  function curl_https($url, $data=array(), $header=array(), $timeout=30){   $ch = curl_init();   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查   curl_setopt($ch, CURLOPT_URL, $url);   curl_setopt($ch, CURLOPT_HTTPHEADER, $header);   curl_setopt($ch, CURLOPT_POST, true);   curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);  $response = curl_exec($ch);  if($error=curl_error($ch)){   die($error);   }  curl_close($ch);  return $response; } ?>

Cache.php
不知道哪位写的源代码~

 cache_expire = $exp_time;  $this -> cache_path = $path; } //returns the filename for the cache private function fileName($key) {  return $this -> cache_path . md5($key); } //creates new cache files with the given data, $key== name of the cache, data the info/values to store public function put($key, $data) {  $values = serialize($data);  $filename = $this -> fileName($key);  $file = fopen($filename, 'w');  if ($file) {//able to create the file   fwrite($file, $values);   fclose($file);  } else   return false; } //returns cache for the given key public function get($key) {  $filename = $this -> fileName($key);  if (!file_exists($filename) || !is_readable($filename)) {//can't read the cache   return false;  }  if (time() < (filemtime($filename) + $this -> cache_expire)) {//cache for the key not expired   $file = fopen($filename, "r");   // read data file   if ($file) {//able to open the file    $data = fread($file, filesize($filename));    fclose($file);    return unserialize($data);    //return the values   } else    return false;  } else   return false;  //was expired you need to create new }}?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

您可能感兴趣的文章:

  • 微信分享调用jssdk实例
  • 详解nodejs微信jssdk后端接口
  • 关于微信jssdk实现多图片上传的一点心得分享
  • 微信+angularJS的SPA应用中用router进行页面跳转,jssdk校验失败问题解决
  • 微信jssdk用法汇总
  • 微信JSSDK多图片上传并且解决IOS系统上传一直加载的问题
  • 微信jssdk在iframe页面失效问题的解决措施
  • 微信JSSDK上传图片
  • 微信JSSDK调用微信扫一扫功能的方法


  • 上一条:
    php 微信开发获取用户信息如何实现
    下一条:
    php微信公众平台开发(四)回复功能开发
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 微信模板消息改版后发送规则记录(微信订阅消息参数值内容限制说明)(1个评论)
    • 微信支付v3对接所需工具及命令(0个评论)
    • 2023年9月1日起:微信小程序必须备案才能上线运营(0个评论)
    • 腾讯官方客服回应了:微信好友上限约10000个!(1个评论)
    • 2023年做微信小程序的老铁注意:新增收费项、微信小程序获取手机号也收费了(2个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-10
    • 2018-01
    • 2020-03
    • 2021-06
    • 2021-10
    • 2022-03
    • 2023-02
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客