1 <?php 2 require_once('share.php');
3 $appid = 'abcdxxxxx'; 4 $APPSECRET = 'abcdxxxxxxx'; 5 $jssdk = new share($appid, $APPSECRET); 6 $signPackage = $jssdk->ec(); 7 ?> 8 9 <!DOCTYPE html> 10 <html lang="en"> 11 <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> 12 <script> 13 //alert(location.href.split('#')[0]) 14 /* 15 *Android 自定义分享接口需升级至 6.0.2.58 版本及以上。 16 * 3. 常见问题及完整 JS-SDK 文档地址:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html 17 * 18 * 开发中遇到问题详见文档“附录5-常见错误及解决办法”解决,如仍未能解决可通过以下渠道反馈: 19 * 邮箱地址:weixin-open@qq.com 20 * 邮件主题:【微信JS-SDK反馈】具体问题 21 * 邮件内容说明:用简明的语言描述问题所在,并交代清楚遇到该问题的场景,可附上截屏图片,微信团队会尽快处理你的反馈。 22 */ 23 24 wx.config({ 25 debug: true, 26 appId: '<?php echo $appid; ?>', 27 timestamp: <?php echo $signPackage["time"]; ?>, 28 nonceStr: '<?php echo $signPackage["rand"]; ?>', 29 signature: '<?php echo $signPackage["signature"]; ?>', 30 jsApiList: [ 31 'checkJsApi', 32 'onMenuShareTimeline', 33 'onMenuShareAppMessage', 34 'onMenuShareQQ', 35 'onMenuShareWeibo', 36 'hideMenuItems', 37 ] 38 }); 39 40 <!--微信分享--> 41 wx.ready(function () 42 { 43 44 wx.onMenuShareTimeline({ 45 title: 'xxxxxx', // 分享标题 46 link: 'xxxxxxxxxx', // 分享链接 47 imgUrl: 'xxxxxxxxxxxxxxx', // 分享图标 48 success: function () { 49 // 用户确认分享后执行的回调函数 50 }, 51 cancel: function () { 52 // 用户取消分享后执行的回调函数 53 } 54 }); 55 var shareData = { 56 title: '标题', 57 desc: '描述', 58 link: 'xxx', 59 imgUrl: 'xxx' 60 }; 61 wx.onMenuShareAppMessage(shareData); 62 wx.onMenuShareTimeline(shareData); 63 wx.onMenuShareQQ(shareData); 64 wx.onMenuShareWeibo(shareData); 65 }); 66 67 wx.error(function (res) 68 { 69 console.log(res.errMsg); 70 }); 71 <!--END 微信分享--> 72 73 </script> 74 </html>
1 <?php 2 3 class share 4 { 5 private $appId; 6 private $appSecret; 7 8 public function __construct($appId, $appSecret) 9 { 10 $this->appId = $appId; 11 $this->appSecret = $appSecret; 12 } 13 14 public function ec() 15 { 16 $res = $this->getAccessToken();//token 17 18 $ticket = $this->getTicket($res);//ticket 19 20 $rand = $this->createNonceStr();//随机字符 21 22 $time = time();//时间戳 23 24 $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; 25 $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; //要分享页面的url 26 27 $arr=array(); 28 29 $arr['ticket'] = $ticket; 30 31 $arr['rand'] = $rand; 32 33 $arr['time'] = $time; 34 35 $arr['url'] = $url; 36 37 $string = "jsapi_ticket={$arr['ticket']}&noncestr={$arr['rand']}×tamp={$arr['time']}&url={$arr['url']}"; //签名排序 38 39 $signature = sha1($string); 40 41 $arr['signature'] = $signature; 42 43 return $arr; 44 } 45 46 //随机字符串 47 private function createNonceStr($length = 16) 48 { 49 50 $chars = "abcdefghijklmnopqrstuvwxyz"; 51 52 $str = ""; 53 54 for ($i = 0; $i < $length; $i++) 55 { 56 57 $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); 58 59 } 60 61 return $str; 62 63 } 64 65 //获取 token 7200 66 private function getAccessToken() 67 { 68 69 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"; 70 71 $data = $this->get_file('token.txt'); 72 73 if(strlen($data)>1)//文件存在 74 { 75 76 $tokenArr = unserialize($data); 77 78 if(time()<$tokenArr['expire']) 79 { 80 81 goto a; 82 83 } 84 else 85 { 86 87 $tokenArr = json_decode($this->httpGet($url),true); 88 89 $tokenArr['expire'] = time()+7000; 90 91 $this->set_file('token.txt',serialize($tokenArr)); 92 93 } 94 } 95 else //文件不存在 96 { 97 $tokenArr = json_decode($this->httpGet($url),true); 98 99 $tokenArr['expire'] = time()+7000; 100 101 $this->set_file('token.txt',serialize($tokenArr)); 102 103 } 104 105 a: 106 return $tokenArr['access_token']; 107 } 108 109 110 111 112 //获取jsapi_ticket 7200 113 private function getTicket($res) 114 { 115 116 $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$res."&type=jsapi"; 117 118 $data = $this->get_file('ticket.txt'); 119 120 if(strlen($data)>1) 121 { 122 123 $ticketArr = unserialize($data); 124 125 if(time() < $ticketArr['expire']) 126 { 127 128 goto a; 129 130 } 131 else 132 { 133 134 $ticketArr = json_decode($this->httpGet($url),true); 135 136 $ticketArr['expire'] = time()+7000; 137 138 $this->set_file('ticket.txt',serialize($ticketArr)); 139 140 } 141 } 142 else 143 { 144 145 $ticketArr = json_decode($this->httpGet($url),true); 146 147 $ticketArr['expire'] = time()+7000; 148 149 $this->set_file('ticket.txt',serialize($ticketArr)); 150 151 } 152 153 a: 154 155 return $ticketArr['ticket']; 156 } 157 158 private function httpGet($url) 159 { 160 161 $curl = curl_init(); 162 163 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 164 165 curl_setopt($curl, CURLOPT_TIMEOUT, 500); 166 167 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); 168 169 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); 170 171 curl_setopt($curl, CURLOPT_URL, $url); 172 173 $res = curl_exec($curl); 174 175 curl_close($curl); 176 177 return $res; 178 179 } 180 181 private function get_file($filename) 182 { 183 184 return trim(file_get_contents($filename)); 185 186 } 187 188 private function set_file($filename, $content) 189 { 190 191 $fp = fopen($filename,"w"); 192 fwrite($fp,$content); 193 fclose($fp); 194 195 } 196 197 }