PHP 调用微信JS-SDK 开发详解 [网摘]
一:准备文件,并将文件置于网站根目录下
access_token.json
{
"access_token"
:
""
,
"expire_time"
:0}
jsapi_ticket.json
{
"jsapi_ticket"
:
""
,
"expire_time"
:0}
下面是一个jssdk的类文件
文件:jssdk.php
1 <?php 2 class JSSDK { 3 private $appId; 4 private $appSecret; 5 public function __construct($appId, $appSecret) { 6 $this->appId = $appId; 7 $this->appSecret = $appSecret; 8 } 9 public function getSignPackage() { 10 $jsapiTicket = $this->getJsApiTicket(); 11 $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 12 $timestamp = time(); 13 $nonceStr = $this->createNonceStr(); 14 // 这里参数的顺序要按照 key 值 ASCII 码升序排序 15 $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; 16 $signature = sha1($string); 17 $signPackage = array( 18 "appId" => $this->appId, 19 "nonceStr" => $nonceStr, 20 "timestamp" => $timestamp, 21 "url" => $url, 22 "signature" => $signature, 23 "rawString" => $string 24 ); 25 return $signPackage; 26 } 27 private function createNonceStr($length = 16) { 28 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 29 $str = ""; 30 for ($i = 0; $i < $length; $i++) { 31 $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); 32 } 33 return $str; 34 } 35 private function getJsApiTicket() { 36 // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例 37 $data = json_decode(file_get_contents("jsapi_ticket.json")); 38 if ($data->expire_time < time()) { 39 $accessToken = $this->getAccessToken(); 40 $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; 41 $res = json_decode($this->httpGet($url)); 42 $ticket = $res->ticket; 43 if ($ticket) { 44 $data->expire_time = time() + 7000; 45 $data->jsapi_ticket = $ticket; 46 $fp = fopen("jsapi_ticket.json", "w"); 47 fwrite($fp, json_encode($data)); 48 fclose($fp); 49 } 50 } else { 51 $ticket = $data->jsapi_ticket; 52 } 53 return $ticket; 54 } 55 private function getAccessToken() { 56 // access_token 应该全局存储与更新,以下代码以写入到文件中做示例 57 $data = json_decode(file_get_contents("access_token.json")); 58 if ($data->expire_time < time()) { 59 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"; 60 $res = json_decode($this->httpGet($url)); 61 $access_token = $res->access_token; 62 if ($access_token) { 63 $data->expire_time = time() + 7000; 64 $data->access_token = $access_token; 65 $fp = fopen("access_token.json", "w"); 66 fwrite($fp, json_encode($data)); 67 fclose($fp); 68 } 69 } else { 70 $access_token = $data->access_token; 71 } 72 return $access_token; 73 } 74 private function httpGet($url) { 75 $curl = curl_init(); 76 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 77 curl_setopt($curl, CURLOPT_TIMEOUT, 500); 78 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 79 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 80 curl_setopt($curl, CURLOPT_URL, $url); 81 $res = curl_exec($curl); 82 curl_close($curl); 83 return $res; 84 } 85 }
二:编辑显示页面对应的后台PHP文件,添加如下代码
1 include('jssdk.php'); 2 $jssdk = new JSSDK("APPID", "APPSECRET"); 3 $signPackage = $jssdk->GetSignPackage(); 4 5 $this->signPackage = $signPackage;
请将APPID和APPSECRET换成您自己微信账号的信息
三:编辑显示页面模板html文件,添加如下代码
1 <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> 2 <script> 3 wx.config({ 4 appId: '{suetech:$signPackage.appId}', 5 timestamp: {suetech:$signPackage.timestamp}, 6 nonceStr: '{suetech:$signPackage.nonceStr}', 7 signature: '{suetech:$signPackage.signature}', 8 jsApiList: [ 9 'checkJsApi', 10 'onMenuShareTimeline', 11 'onMenuShareAppMessage', 12 'onMenuShareQQ', 13 'onMenuShareWeibo' 14 ] 15 }); 16 /* 17 * 注意: 18 * 1. 所有的JS接口只能在公众号绑定的域名下调用,公众号开发者需要先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。 19 * 2. 如果发现在 Android 不能分享自定义内容,请到官网下载最新的包覆盖安装,Android 自定义分享接口需升级至 6.0.2.58 版本及以上。 20 * 3. 完整 JS-SDK 文档地址:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html 21 * 22 * 如有问题请通过以下渠道反馈: 23 * 邮箱地址:weixin-open@qq.com 24 * 邮件主题:【微信JS-SDK反馈】具体问题 25 * 邮件内容说明:用简明的语言描述问题所在,并交代清楚遇到该问题的场景,可附上截屏图片,微信团队会尽快处理你的反馈。 26 */ 27 wx.ready(function () { 28 // 1 判断当前版本是否支持指定 JS 接口,支持批量判断 29 wx.checkJsApi({ 30 jsApiList: [ 31 'getNetworkType', 32 'previewImage', 33 'onMenuShareTimeline', 34 'onMenuShareAppMessage', 35 'onMenuShareQQ', 36 'onMenuShareWeibo' 37 ], 38 success: function (res) { 39 alert(JSON.stringify(res)); 40 } 41 }); 42 var shareData = { 43 title: '标题', 44 desc: '描述', 45 link: '链接', 46 imgUrl: '图片', 47 trigger: function (res) { 48 alert('用户点击发送给朋友'); 49 }, 50 success: function (res) { 51 alert('已分享'); 52 }, 53 cancel: function (res) { 54 alert('已取消'); 55 }, 56 fail: function (res) { 57 alert(JSON.stringify(res)); 58 } 59 }; 60 wx.onMenuShareAppMessage(shareData); 61 wx.onMenuShareTimeline(shareData); 62 wx.onMenuShareQQ(shareData); 63 wx.onMenuShareWeibo(shareData); 64 }); 65 wx.error(function (res) { 66 alert(res.errMsg); 67 }); 68 </script>
到此已经能够正常进行微信自定义分享了
需要注意的地方是在微信公众账号中配置您的域名,设置方法:
登陆微信公众账号->公众号设置->功能设置->JS接口安全域名->设置
来源:开源中国
来源 access_token.json
1
|
{ "access_token" : "" , "expire_time" :0} |
jsapi_ticket.json
1
|
{ "jsapi_ticket" : "" , "expire_time" :0} |