首頁 >php教程 >php手册 >微信JSSDK分享

微信JSSDK分享

WBOY
WBOY原創
2016-06-07 11:39:331242瀏覽

thinkphp与微信JSSDK结合分享实例
新建CommonController.class.php来获取
appId: '', // 必填,公众号的唯一标识
timestamp: , // 必填,生成签名的时间戳
nonceStr: '', // 必填,生成签名的随机串
signature: '',// 必填,签名<?php <br /> namespace Home\Controller;<br> use Think\Controller;<br> <br> <br> class CommonController extends Controller {<br>     public function _initialize()<br>     {<br>         $jssdk = new JSSDK("您的ID", "您的ACCESS");<br>         $signPackage = $jssdk->GetSignPackage();<br>         $this->assign('signPackage',$signPackage);<br>     }<br> }<br> <br> //微信方法<br> <br> class JSSDK {<br>     private $appId;<br>     private $appSecret;<br>     public function __construct($appId, $appSecret) {<br>       $this->appId = $appId;<br>       $this->appSecret = $appSecret;<br>     }<br>     public function getSignPackage() {<br>       $jsapiTicket = $this->getJsApiTicket();<br>       $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";<br>       $timestamp = time();<br>       $nonceStr = $this->createNonceStr();<br>       // 这里参数的顺序要按照 key 值 ASCII 码升序排序<br>       $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";<br>       $signature = sha1($string);<br>       $signPackage = array(<br>         "appId"     => $this->appId,<br>         "nonceStr"  => $nonceStr,<br>         "timestamp" => $timestamp,<br>         "url"       => $url,<br>         "signature" => $signature,<br>         "rawString" => $string<br>       );<br>       return $signPackage; <br>     }<br>     private function createNonceStr($length = 16) {<br>       $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";<br>       $str = "";<br>       for ($i = 0; $i          $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);<br>       }<br>       return $str;<br>     }<br>     private function getJsApiTicket() {<br>       // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例<br>       $data = json_decode(file_get_contents("jsapi_ticket.json"));<br>       if ($data->expire_time          $accessToken = $this->getAccessToken();<br>         $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";<br>         $res = json_decode($this->httpGet($url));<br>         $ticket = $res->ticket;<br>         if ($ticket) {<br>           $data->expire_time = time() + 7000;<br>           $data->jsapi_ticket = $ticket;<br>           $fp = fopen("jsapi_ticket.json", "w");<br>           fwrite($fp, json_encode($data));<br>           fclose($fp);<br>         }<br>       } else {<br>         $ticket = $data->jsapi_ticket;<br>       }<br>       return $ticket;<br>     }<br>     private function getAccessToken() {<br>       // access_token 应该全局存储与更新,以下代码以写入到文件中做示例<br>       $data = json_decode(file_get_contents("access_token.json"));<br>       if ($data->expire_time          $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";<br>         $res = json_decode($this->httpGet($url));<br>         $access_token = $res->access_token;<br>         if ($access_token) {<br>           $data->expire_time = time() + 7000;<br>           $data->access_token = $access_token;<br>           $fp = fopen("access_token.json", "w");<br>           fwrite($fp, json_encode($data));<br>           fclose($fp);<br>         }<br>       } else {<br>         $access_token = $data->access_token;<br>       }<br>       return $access_token;<br>     }<br>     private function httpGet($url) {<br>       $curl = curl_init();<br>       curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);<br>       curl_setopt($curl, CURLOPT_TIMEOUT, 500);<br>       curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);<br>       curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);<br>       curl_setopt($curl, CURLOPT_URL, $url);<br>       $res = curl_exec($curl);<br>       curl_close($curl);<br>       return $res;<br>     }<br> }下面就可以使用了
新建IndexController.class.php<?php <br /> namespace Home\Controller;<br> use Think\Controller;<br> <br> class IndexController extends CommonController {<br>     public function index(){        <br>         $this->display();<br>     }<br> <br> }html文件中添加<script></script><br>         <script><br /> wx.config({<br /> appId: &#039;{$signPackage.appId}&#039;,<br /> timestamp: {$signPackage.timestamp},<br /> nonceStr: &#039;{$signPackage.nonceStr}&#039;,<br /> signature: &#039;{$signPackage.signature}&#039;,<br /> jsApiList: [<br /> &#039;checkJsApi&#039;,<br /> &#039;onMenuShareTimeline&#039;,<br /> &#039;onMenuShareAppMessage&#039;,<br /> &#039;onMenuShareQQ&#039;,<br /> &#039;onMenuShareWeibo&#039;<br /> ]<br /> }); <br /> wx.ready(function () {<br /> // 1 判断当前版本是否支持指定 JS 接口,支持批量判断<br /> wx.checkJsApi({<br /> jsApiList: [<br /> &#039;getNetworkType&#039;,<br /> &#039;previewImage&#039;,<br /> &#039;onMenuShareTimeline&#039;,<br /> &#039;onMenuShareAppMessage&#039;,<br /> &#039;onMenuShareQQ&#039;,<br /> &#039;onMenuShareWeibo&#039;<br /> ], <br /> });<br /> <br /> var shareData = {<br /> //标题<br /> title: &#039;标题&#039;,<br /> //摘要<br /> desc: &#039;摘要&#039;,<br /> //链接,可以换主页<br /> link: &#039;{$signPackage.url}&#039;,<br /> //缩略图<br /> imgUrl: &#039;缩略图&#039;, <br /> <br /> };<br /> wx.onMenuShareAppMessage(shareData);<br /> wx.onMenuShareTimeline(shareData);<br /> wx.onMenuShareQQ(shareData);<br /> wx.onMenuShareWeibo(shareData);<br /> });<br /> </script>之后就大功告成

AD:真正免费,域名+虚机+企业邮箱=0元

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn