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

 更新时间:2016年12月12日 14:50:18   作者:乡下程序员  
这篇文章主要为大家详细介绍了php如何利用一个文件搞定微信jssdk配置,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

<?php
 include "lib/Cache.php";
 define($APPID, "……");
 define($SECRET, "……")
 if($_GET['Type'] == "access_token"){
//  echo getAccess_token();
 }
 else if($_GET['Type'] == "jsapi_ticket"){
//  echo getJsapi_ticket();
 }
 else if($_GET['Type'] == "config"){
  $jsapi_ticket = getJsapi_ticket();
  $nonceStr = "x".rand(10000,100000)."x"; //随机字符串
  $timestamp = time(); //时间戳
  $url = $_GET['url'];
  $signature = getSignature($jsapi_ticket,$nonceStr, $timestamp, $url);

  $result = array("jsapi_ticket"=>$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."&timestamp=".$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 要發送的數據 
 * @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
不知道哪位写的源代码~

<?php
class Cache {
 private $cache_path;
 //path for the cache
 private $cache_expire;
 //seconds that the cache expires

 //cache constructor, optional expiring time and cache path
 public function Cache($exp_time = 3600, $path = "cache/") {
  $this -> 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
 }

}
?>

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

相关文章

  • php实现的用户查询类实例

    php实现的用户查询类实例

    这篇文章主要介绍了php实现的用户查询类,实例分析了php针对用户查询与判断的相关技巧,需要的朋友可以参考下
    2015-06-06
  • phpmail类发送邮件函数代码

    phpmail类发送邮件函数代码

    前天要给网站做一个小功能,就是在用户留言的时候发邮件通知管理员。我们都知道在PHP里有一个mail函数,但是要求服务器要有邮件服务器的功能,比如简单的SMTP或者POP3。但是,如果我们的服务器没有这种功能,那么怎么办呢
    2012-02-02
  • php基于session实现数据库交互的类实例

    php基于session实现数据库交互的类实例

    这篇文章主要介绍了php基于session实现数据库交互的类,实例分析了php结合session操作数据库的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • PHP缓存机制Output Control详解

    PHP缓存机制Output Control详解

    这篇文章主要介绍了PHP缓存机制Output Control,需要的朋友可以参考下
    2014-07-07
  • php stripslashes和addslashes的区别

    php stripslashes和addslashes的区别

    这篇文章主要介绍了php stripslashes和addslashes的区别,特分享下方便需要的朋友。
    2014-02-02
  • 增加反向链接的101个方法 站长推荐

    增加反向链接的101个方法 站长推荐

    增加反向链接的101个方法 站长推荐...
    2007-01-01
  • PHP实现的增强性mhash函数

    PHP实现的增强性mhash函数

    这篇文章主要介绍了PHP实现的增强性mhash函数,使用默认mhash函数时报错,找到了两个解决方法,需要的朋友可以参考下
    2015-05-05
  • PHP 输出缓存详解

    PHP 输出缓存详解

    Output Control 函数可以让你自由控制脚本中数据的输出。它非常地有用,特别是对于:当你想在数据已经输出后,再输出文件头的情况。
    2009-06-06
  • php 文本文件的读取效率

    php 文本文件的读取效率

    一直鼓捣着想把博客的首页存在文件里,结果存在文件里,博客更新时再重新将内容再次写入到这个文件。所以刚才测试了三种读取文本的常规方法
    2012-02-02
  • 深入PHP获取随机数字和字母的方法详解

    深入PHP获取随机数字和字母的方法详解

    本篇文章是对PHP获取随机数字和字母的方法进行了详细的分析介绍,需要的朋友参考下
    2013-06-06

最新评论