博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
微信JS-SDK实现自定义分享功能分享
阅读量:5107 次
发布时间:2019-06-13

本文共 5031 字,大约阅读时间需要 16 分钟。

1 
appId = $appId; 9 $this->appSecret = $appSecret; 10 } 11 12 public function getSignPackage() { 13 $jsapiTicket = $this->getJsApiTicket(); 14 15 // 注意 URL 一定要动态获取,不能 hardcode. 16 $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; 17 $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 18 19 $timestamp = time(); 20 $nonceStr = $this->createNonceStr(); 21 22 // 这里参数的顺序要按照 key 值 ASCII 码升序排序 23 $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url"; 24 25 $signature = sha1($string); 26 27 $signPackage = array( 28 "appId" => $this->appId, 29 "nonceStr" => $nonceStr, 30 "timestamp" => $timestamp, 31 "url" => $url, 32 "signature" => $signature, 33 "rawString" => $string 34 ); 35 return $signPackage; 36 } 37 38 private function createNonceStr($length = 16) { 39 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 40 $str = ""; 41 for ($i = 0; $i < $length; $i++) { 42 $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); 43 } 44 return $str; 45 } 46 47 private function getJsApiTicket() { 48 // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例 49 $data = json_decode($this->get_php_file(APP_PATH."jsapi_ticket.php")); 50 if ($data->expire_time < time()) { 51 $accessToken = $this->getAccessToken(); 52 file_put_contents(APP_PATH."accessToken.txt",$accessToken); 53 if($accessToken){ 54 // 如果是企业号用以下 URL 获取 ticket 55 // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; 56 $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; 57 $res = json_decode($this->httpGet($url)); 58 $ticket = $res->ticket; 59 if ($ticket) { 60 $data->expire_time = time() + 7000; 61 $data->jsapi_ticket = $ticket; 62 $this->set_php_file(APP_PATH."jsapi_ticket.php", json_encode($data)); 63 } 64 } 65 } else { 66 $ticket = $data->jsapi_ticket; 67 } 68 69 return $ticket; 70 } 71 72 private function getAccessToken() { 73 // access_token 应该全局存储与更新,以下代码以写入到文件中做示例 74 $data = json_decode($this->get_php_file(APP_PATH."access_token.php")); 75 //file_put_contents(APP_PATH."data.txt",$data); 76 if ($data->expire_time < time()) { 77 // 如果是企业号用以下URL获取access_token 78 // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret"; 79 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"; 80 file_put_contents(APP_PATH."url_acc.txt",$url); 81 $res = json_decode($this->httpGet($url)); 82 //file_put_contents(APP_PATH."res.txt",$res); 83 $access_token = $res->access_token; 84 if ($access_token) { 85 $data->expire_time = time() + 7000; 86 $data->access_token = $access_token; 87 $this->set_php_file(APP_PATH."access_token.php", json_encode($data)); 88 } 89 } else { 90 $access_token = $data->access_token; 91 } 92 return $access_token; 93 } 94 95 private function httpGet($url) { 96 file_put_contents(APP_PATH."url_http.txt",$url); 97 $curl = curl_init(); 98 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 99 curl_setopt($curl, CURLOPT_TIMEOUT, 500);100 // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。101 // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。102 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);103 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);104 curl_setopt($curl, CURLOPT_URL, $url);105 106 $res = curl_exec($curl);107 curl_close($curl);108 109 return $res;110 }111 112 private function get_php_file($filename) {113 return trim(substr(file_get_contents($filename), 15));114 }115 private function set_php_file($filename, $content) {116 $fp = fopen($filename, "w");117 fwrite($fp, "
" . $content);118 fclose($fp);119 }120 }
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

  我也忘记在哪里看来的,分享接口用的是jweixin-1.0.0.js, onMenuShareTimeline 即将废除 但也不影响使用,废除以后换一下就ok.目前可以用.具体查看https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115

类中txt文档不用写入也没有事

 

 

转载于:https://www.cnblogs.com/ssx314/p/10613598.html

你可能感兴趣的文章
一道不知道哪里来的容斥题
查看>>
Blender Python UV 学习
查看>>
window添加右键菜单
查看>>
入手腾龙SP AF90mm MACRO
查看>>
Window7上搭建symfony开发环境(PEAR)
查看>>
Linux内核态、用户态简介与IntelCPU特权级别--Ring0-3
查看>>
第23月第24天 git命令 .git-credentials git rm --cached git stash clear
查看>>
java SE :标准输入/输出
查看>>
一些方便系统诊断的bash函数
查看>>
jquery中ajax返回值无法传递到上层函数
查看>>
css3之transform-origin
查看>>
[转]JavaScript快速检测浏览器对CSS3特性的支持
查看>>
Master选举原理
查看>>
[ JAVA编程 ] double类型计算精度丢失问题及解决方法
查看>>
小别离
查看>>
微信小程序-发起 HTTPS 请求
查看>>
WPF动画设置1(转)
查看>>
基于node/mongo的App Docker化测试环境搭建
查看>>
秒杀9种排序算法(JavaScript版)
查看>>
Activiti入门 -- 环境搭建和核心API简介
查看>>