当前位置:Gxlcms > PHP教程 > php一个文件搞定微信jssdk配置的实例代码详解

php一个文件搞定微信jssdk配置的实例代码详解

时间:2021-07-01 10:21:17 帮助过:35人阅读

php一个文件搞定微信jssdk配置:
包括缓存,包括https通讯,获取微信access_token,签名什么的都有。但是防范性编程做得比较少,商业用的话,需要完善下代码。
使用姿势

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

服务端
GetWX.php

  1. <?php
  2. include "lib/Cache.php";
  3. define($APPID, "……");
  4. define($SECRET, "……")
  5. if($_GET['Type'] == "access_token"){
  6. // echo getAccess_token();
  7. }
  8. else if($_GET['Type'] == "jsapi_ticket"){
  9. // echo getJsapi_ticket();
  10. }
  11. else if($_GET['Type'] == "config"){
  12. $jsapi_ticket = getJsapi_ticket();
  13. $nonceStr = "x".rand(10000,100000)."x"; //随机字符串
  14. $timestamp = time(); //时间戳
  15. $url = $_GET['url'];
  16. $signature = getSignature($jsapi_ticket,$nonceStr, $timestamp, $url);
  17. $result = array("jsapi_ticket"=>$jsapi_ticket, "nonceStr"=>$nonceStr,"timestamp"=>$timestamp,"url"=>$url,"signature"=>$signature);
  18. echo json_encode($result);
  19. }
  20. function getSignature($jsapi_ticket,$noncestr, $timestamp, $url){
  21. $string1 = "jsapi_ticket=".$jsapi_ticket."&noncestr=".$noncestr."&timestamp=".$timestamp."&url=".$url;
  22. $sha1 = sha1($string1);
  23. return $sha1;
  24. }
  25. function getJsapi_ticket(){
  26. $cache = new Cache();
  27. $cache = new Cache(7000, 'cache/'); //需要创建cache文件夹存储缓存文件。
  28. //从缓存从读取键值 $key 的数据
  29. $jsapi_ticket = $cache -> get("jsapi_ticket");
  30. $access_token = getAccess_token();
  31. //如果没有缓存数据
  32. if ($jsapi_ticket == false) {
  33. $access_token = getAccess_token();
  34. $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket';
  35. $data = array('type'=>'jsapi','access_token'=>$access_token);
  36. $header = array();
  37. $response = json_decode(curl_https($url, $data, $header, 5));
  38. $jsapi_ticket = $response->ticket;
  39. //写入键值 $key 的数据
  40. $cache -> put("jsapi_ticket", $jsapi_ticket);
  41. }
  42. return $jsapi_ticket;
  43. }
  44. function getAccess_token(){
  45. $cache = new Cache();
  46. $cache = new Cache(7000, 'cache/');
  47. //从缓存从读取键值 $key 的数据
  48. $access_token = $cache -> get("access_token");
  49. //如果没有缓存数据
  50. if ($access_token == false) {
  51. $url = 'https://api.weixin.qq.com/cgi-bin/token';
  52. $data = array('grant_type'=>'client_credential','appid'=>$APPID,'secret'=>$SECRET);
  53. $header = array();
  54. $response = json_decode(curl_https($url, $data, $header, 5));
  55. $access_token = $response->access_token;
  56. //写入键值 $key 的数据
  57. $cache -> put("access_token", $access_token);
  58. }
  59. return $access_token;
  60. }
  61. /** curl 获取 https 请求
  62. * @param String $url 请求的url
  63. * @param Array $data 要發送的數據
  64. * @param Array $header 请求时发送的header
  65. * @param int $timeout 超时时间,默认30s
  66. */
  67. function curl_https($url, $data=array(), $header=array(), $timeout=30){
  68. $ch = curl_init();
  69. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  70. curl_setopt($ch, CURLOPT_URL, $url);
  71. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  72. curl_setopt($ch, CURLOPT_POST, true);
  73. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  74. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  75. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  76. $response = curl_exec($ch);
  77. if($error=curl_error($ch)){
  78. die($error);
  79. }
  80. curl_close($ch);
  81. return $response;
  82. }
  83. ?>

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

  1. <?php
  2. class Cache {
  3. private $cache_path;
  4. //path for the cache
  5. private $cache_expire;
  6. //seconds that the cache expires
  7. //cache constructor, optional expiring time and cache path
  8. public function Cache($exp_time = 3600, $path = "cache/") {
  9. $this -> cache_expire = $exp_time;
  10. $this -> cache_path = $path;
  11. }
  12. //returns the filename for the cache
  13. private function fileName($key) {
  14. return $this -> cache_path . md5($key);
  15. }
  16. //creates new cache files with the given data, $key== name of the cache, data the info/values to store
  17. public function put($key, $data) {
  18. $values = serialize($data);
  19. $filename = $this -> fileName($key);
  20. $file = fopen($filename, 'w');
  21. if ($file) {//able to create the file
  22. fwrite($file, $values);
  23. fclose($file);
  24. } else
  25. return false;
  26. }
  27. //returns cache for the given key
  28. public function get($key) {
  29. $filename = $this -> fileName($key);
  30. if (!file_exists($filename) || !is_readable($filename)) {//can't read the cache
  31. return false;
  32. }
  33. if (time() < (filemtime($filename) + $this -> cache_expire)) {//cache for the key not expired
  34. $file = fopen($filename, "r");
  35. // read data file
  36. if ($file) {//able to open the file
  37. $data = fread($file, filesize($filename));
  38. fclose($file);
  39. return unserialize($data);
  40. //return the values
  41. } else
  42. return false;
  43. } else
  44. return false;
  45. //was expired you need to create new
  46. }
  47. }
  48. ?>

以上就是php一个文件搞定微信jssdk配置的实例代码详解的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!

人气教程排行