Home > php教程 > php手册 > 微信JSSDK支持命名空间版本(3.2)

微信JSSDK支持命名空间版本(3.2)

WBOY
Release: 2016-06-07 11:39:54
Original
1267 people have browsed it

昨天微信提供了新版本的jssdk,这里修改了一下,支持了3.2版本的命令空间,同时添加了调试模式!
<?php <br /> /**<br>  * 官方文档:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html<br>  * 官方示例:http://demo.open.weixin.qq.com/jssdk/sample.zip<br>  * <br>  * 微信JSSDK类,主要修改了保存会话信息机制,示例中使用的是文件,这里使用了ThinkPHP的缓存机制,参考官方提供的示例文档 <br>  * 新增了调试模式,调用示例如下:<br>  *         $jssdk = new JSSDK(C('WX_APPID'), C('WX_SECRET'));<br>  *      $jssdk->debug = true;    //启用本地调试模式,将官方的两个json文件放到入口文件index.php同级目录即可!<br>  *       $signPackage = $jssdk->GetSignPackage();<br>  * @命名空间版本<br>  * @author 阿甘 (QQ:33808 624)<br>  * @date 2015-1-10 14:10<br>  */<br> namespace Com;<br> <br> class JsSdk {<br>   private $appId;<br>   private $appSecret;<br>   public $debug = false;<br> <br>   public function __construct($appId, $appSecret) {<br>     $this->appId = $appId;<br>     $this->appSecret = $appSecret;<br>   }<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> <br>     // 这里参数的顺序要按照 key 值 ASCII 码升序排序<br>     $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";<br> <br>     $signature = sha1($string);<br> <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> <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> <br>   private function getJsApiTicket() {<br>       //debug模式<br>       if ($this->debug) {<br>         // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例<br>         $data = json_decode(file_get_contents("jsapi_ticket.json"));<br>       } else {<br>           //从cache中读取,基于ThinkPHP的缓存机制<br>           $data = (object)(S('jsapi_ticket_json'));<br>       }<br> <br>     if ($data->expire_time        $accessToken = $this->getAccessToken();<br>       $url = "http://api.weixin.qq.com/cgi-bin/ticket/getticket?type=1&access_token=$accessToken";<br>       $res = json_decode($this->httpGet($url));<br>       $ticket = $res->ticket;<br>       <br>       if ($ticket) {<br>         $data->expire_time = time() + 7200;<br>         $data->jsapi_ticket = $ticket;<br>                <br>         //debug模式<br>         if ($this->debug) {<br>             $fp = fopen("jsapi_ticket.json", "w");<br>             fwrite($fp, json_encode($data));<br>             fclose($fp);<br>         } else {<br>             //将对象以数组的形式进行缓存<br>             S('jsapi_ticket_json', (array)$data);<br>         }<br> <br>       }<br>     } else {<br>       $ticket = $data->jsapi_ticket;<br>     }<br> <br>     return $ticket;<br>   }<br> <br>   private function getAccessToken() {<br> <br>       //debug模式<br>       if ($this->debug) {<br>         // access_token 应该全局存储与更新,以下代码以写入到文件中做示例<br>           $data = json_decode(file_get_contents("access_token.json"));<br>           dump($data);<br>           die();<br>       } else {<br>           //从缓存中读取数组并转成对象<br>         $data = (Object)(S('access_token.json'));<br>       }<br>     <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> <br>       if ($access_token) {<br>         $data->expire_time = time() + 7000;<br>         $data->access_token = $access_token;<br> <br>         //debug模式<br>         if ($this->debug) {<br>             $fp = fopen("access_token.json", "w");<br>             fwrite($fp, json_encode($data));<br>             fclose($fp);<br>         } else {<br>             //缓存数组<br>             S('access_token.json', (array)$data);            <br>         }<br>         <br>       }<br>     } else {<br>       $access_token = $data->access_token;<br>     }<br>     return $access_token;<br>   }<br> <br>   private function httpGet($url) {<br>       <br>     $curl = curl_init();<br>     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);<br>     curl_setopt($curl, CURLOPT_TIMEOUT, 500);<br>     curl_setopt($curl, CURLOPT_URL, $url);<br> <br>     $res = curl_exec($curl);<br>     <br>     //错误检测<br>     $error = curl_error($curl);<br>     <br>     curl_close($curl);<br>     <br>     //发生错误,抛出异常<br>     if($error) throw new \Exception('请求发生错误(表检查是否在授权域名下访问):' . $error);<br>     <br>     return $res;<br>   }<br>   <br>     <br> }

附件 Com.rar ( 2.28 KB 下载:202 次 )

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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template