首页新闻找找看学习计划

没豆了,厚着脸皮求救火。PHP的朋友速来

0
[已解决问题] 解决于 2015-09-02 00:12

因为想自己调试微信jssdk,但公司都是java,我想整到新浪sae上测试,那只能用php,官方提供的php包不适合sae,因为新浪sae不能读写。。。只能搞到memcache,把原代码json文件的值写到缓存,不要文件了。php不会啊,求帮忙改下下面的文件,以便适应sae配置jssdk。

链接中是新浪sae的memcache资料:http://www.sinacloud.com/doc/sae/php/memcache.html

  1 <?php
  2 
  3 $appId = 'wx8d15d096b84a5039';
  4 $appsecret = '6083f85485f9c253d8c9fc7674b0dbc8';
  5 
  6 $timestamp = time();
  7 $jsapi_ticket = make_ticket($appId,$appsecret);
  8 $nonceStr = make_nonceStr();
  9 $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
 10 $signature = make_signature($nonceStr,$timestamp,$jsapi_ticket,$url);
 11 
 12 function make_nonceStr()
 13 {
 14   $codeSet = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
 15   for ($i = 0; $i<16; $i++) {
 16     $codes[$i] = $codeSet[mt_rand(0, strlen($codeSet)-1)];
 17   }
 18   $nonceStr = implode($codes);
 19   return $nonceStr;
 20 }
 21 
 22 function make_signature($nonceStr,$timestamp,$jsapi_ticket,$url)
 23 {
 24   $tmpArr = array(
 25   'noncestr' => $nonceStr,
 26   'timestamp' => $timestamp,
 27   'jsapi_ticket' => $jsapi_ticket,
 28   'url' => $url
 29   );
 30   ksort($tmpArr, SORT_STRING);
 31   $string1 = http_build_query( $tmpArr );
 32   $string1 = urldecode( $string1 );
 33   $signature = sha1( $string1 );
 34   return $signature;
 35 }
 36 
 37 function make_ticket($appId,$appsecret)
 38 {
 39   // access_token 应该全局存储与更新,以下代码以写入到文件中做示例
 40   $data = json_decode(file_get_contents("access_token.json"));
 41   if ($data->expire_time < time()) {
 42     $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appsecret;
 43     $json = file_get_contents($TOKEN_URL);
 44     $result = json_decode($json,true);
 45     $access_token = $result['access_token'];
 46     if ($access_token) {
 47       $data->expire_time = time() + 7000;
 48       $data->access_token = $access_token;
 49       $fp = fopen("access_token.json", "w");
 50       fwrite($fp, json_encode($data));
 51       fclose($fp);
 52     }
 53   }else{
 54     $access_token = $data->access_token;
 55   }
 56 
 57   // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
 58   $data = json_decode(file_get_contents("jsapi_ticket.json"));
 59   if ($data->expire_time < time()) {
 60     $ticket_URL="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi";
 61     $json = file_get_contents($ticket_URL);
 62     $result = json_decode($json,true);
 63     $ticket = $result['ticket'];
 64     if ($ticket) {
 65       $data->expire_time = time() + 7000;
 66       $data->jsapi_ticket = $ticket;
 67       $fp = fopen("jsapi_ticket.json", "w");
 68       fwrite($fp, json_encode($data));
 69       fclose($fp);
 70     }
 71   }else{
 72     $ticket = $data->jsapi_ticket;
 73   }
 74 
 75   return $ticket;
 76 }
 77 
 78 ?>
 79 <!DOCTYPE html>
 80 <html>
 81 <head>
 82   <meta charset="utf-8">
 83   <title>微信JS-SDK Demo</title>
 84   <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
 85   <link rel="stylesheet" href="style.css">
 86 </head>
 87 <body ontouchstart="">
 88 这个页面是demo页面。
 89 认证服务号已测试成功。
 90 记得现在后台设置已备案的安全域名。
 91 </body>
 92 <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
 93 <script>
 94   wx.config({
 95     debug: true,
 96     appId: '<?=$appId?>',
 97     timestamp: <?=$timestamp?>,
 98     nonceStr: '<?=$nonceStr?>',
 99     signature: '<?=$signature?>',
100     jsApiList: [
101       'checkJsApi',
102       'onMenuShareTimeline',
103       'onMenuShareAppMessage',
104       'onMenuShareQQ',
105       'onMenuShareWeibo',
106       'hideMenuItems',
107       'showMenuItems',
108       'hideAllNonBaseMenuItem',
109       'showAllNonBaseMenuItem',
110       'translateVoice',
111       'startRecord',
112       'stopRecord',
113       'onRecordEnd',
114       'playVoice',
115       'pauseVoice',
116       'stopVoice',
117       'uploadVoice',
118       'downloadVoice',
119       'chooseImage',
120       'previewImage',
121       'uploadImage',
122       'downloadImage',
123       'getNetworkType',
124       'openLocation',
125       'getLocation',
126       'hideOptionMenu',
127       'showOptionMenu',
128       'closeWindow',
129       'scanQRCode',
130       'chooseWXPay',
131       'openProductSpecificView',
132       'addCard',
133       'chooseCard',
134       'openCard'
135       ]
136   });
137 
138   wx.ready(function () {
139     var shareData = {
140       title: '这里是分享标题',
141       desc: '这里是发送给好友的时候的简介',
142       link: 'http://baidu.com',
143       imgUrl: 'http://baidu.com/logo.jpg'
144     };
145     wx.onMenuShareAppMessage(shareData);
146     wx.onMenuShareTimeline(shareData);
147   });
148 
149   wx.error(function (res) {
150     alert(res.errMsg);
151   });
152 </script>
153 </html>
酸番茄的主页 酸番茄 | 初学一级 | 园豆:87
提问于:2015-09-01 23:41
< >
分享
最佳答案
0
  1 <?php
  2 
  3 $appId = 'wx8d15d096b84a5039';
  4 $appsecret = '6083f85485f9c253d8c9fc7674b0dbc8 ';
  5 
  6 
  7 
  8 $timestamp = time();
  9 $jsapi_ticket = make_ticket($appId,$appsecret);
 10 $nonceStr = make_nonceStr();
 11 $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
 12 $signature = make_signature($nonceStr,$timestamp,$jsapi_ticket,$url);
 13 
 14 function make_nonceStr()
 15 {
 16   $codeSet = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
 17   for ($i = 0; $i<16; $i++) {
 18     $codes[$i] = $codeSet[mt_rand(0, strlen($codeSet)-1)];
 19   }
 20   $nonceStr = implode($codes);
 21   return $nonceStr;
 22 }
 23 
 24 function make_signature($nonceStr,$timestamp,$jsapi_ticket,$url)
 25 {
 26   $tmpArr = array(
 27   'noncestr' => $nonceStr,
 28   'timestamp' => $timestamp,
 29   'jsapi_ticket' => $jsapi_ticket,
 30   'url' => $url
 31   );
 32   ksort($tmpArr, SORT_STRING);
 33   $string1 = http_build_query( $tmpArr );
 34   $string1 = urldecode( $string1 );
 35   $signature = sha1( $string1 );
 36   return $signature;
 37 }
 38 
 39 function make_ticket($appId,$appsecret)
 40 {
 41   // access_token 应该全局存储与更新,以下代码以写入到文件中做示例
 42   $mmc=memcache_init();
 43   $data = json_decode(memcache_get($mmc,"keycode"));
 44   if ($data->expire_time < time()) {
 45     $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appsecret;
 46     $json = file_get_contents($TOKEN_URL);
 47     $result = json_decode($json,true);
 48     $access_token = $result['access_token'];
 49     if ($access_token) {
 50       $data->expire_time = time() + 7000;
 51       $data->access_token = $access_token;
 52       if($mmc==false){
 53           echo "mc init failed\n";
 54       }
 55       else
 56       {
 57           memcache_set($mmc,"keycode",json_encode($data));//这里的KEY随便你自己命名
 58           //echo memcache_get($mmc,"key");//这里就可以根据KEY来取得你缓存的值了
 59       }
 60 
 61       // $fp = fopen("access_token.json", "w");
 62       // fwrite($fp, json_encode($data));
 63       // fclose($fp);
 64     }
 65   }else{
 66     $access_token = $data->access_token;
 67   }
 68 
 69   // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
 70   $data = json_decode(memcache_get($mmc,"tiketcode"));
 71   if ($data->expire_time < time()) {
 72     $ticket_URL="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi";
 73     $json = file_get_contents($ticket_URL);
 74     $result = json_decode($json,true);
 75     $ticket = $result['ticket'];
 76     if ($ticket) {
 77       $data->expire_time = time() + 7000;
 78       $data->jsapi_ticket = $ticket;
 79 
 80       if($mmc==false)
 81           echo "mc init failed\n";
 82       else
 83       {
 84           memcache_set($mmc,"tiketcode",json_encode($data));//这里的KEY随便你自己命名
 85           //echo memcache_get($mmc,"key");//这里就可以根据KEY来取得你缓存的值了
 86       }
 87       
 88       /*$fp = fopen("jsapi_ticket.json", "w");
 89       fwrite($fp, json_encode($data));
 90       fclose($fp);*/
 91     }
 92   }else{
 93     $ticket = $data->jsapi_ticket;
 94   }
 95 
 96   return $ticket;
 97 }
 98 
 99 ?>
100 <!DOCTYPE html>
101 <html>
102 <head>
103   <meta charset="utf-8">
104   <title>微信JS-SDK Demo</title>
105   <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
106   <link rel="stylesheet" href="style.css">
107 </head>
108 <body ontouchstart="">
109 这个页面是demo页面。
110 认证服务号已测试成功。
111 记得现在后台设置已备案的安全域名。
112 </body>
113 <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
114 <script>
115   wx.config({
116     debug: true,
117     appId: '<?=$appId?>',
118     timestamp: <?=$timestamp?>,
119     nonceStr: '<?=$nonceStr?>',
120     signature: '<?=$signature?>',
121     jsApiList: [
122       'checkJsApi',
123       'onMenuShareTimeline',
124       'onMenuShareAppMessage',
125       'onMenuShareQQ',
126       'onMenuShareWeibo',
127       'hideMenuItems',
128       'showMenuItems',
129       'hideAllNonBaseMenuItem',
130       'showAllNonBaseMenuItem',
131       'translateVoice',
132       'startRecord',
133       'stopRecord',
134       'onRecordEnd',
135       'playVoice',
136       'pauseVoice',
137       'stopVoice',
138       'uploadVoice',
139       'downloadVoice',
140       'chooseImage',
141       'previewImage',
142       'uploadImage',
143       'downloadImage',
144       'getNetworkType',
145       'openLocation',
146       'getLocation',
147       'hideOptionMenu',
148       'showOptionMenu',
149       'closeWindow',
150       'scanQRCode',
151       'chooseWXPay',
152       'openProductSpecificView',
153       'addCard',
154       'chooseCard',
155       'openCard'
156       ]
157   });
158 
159   wx.ready(function () {
160     var shareData = {
161       title: '这里是分享标题',
162       desc: '这里是发送给好友的时候的简介',
163       link: 'http://baidu.com',
164       imgUrl: 'http://baidu.com/logo.jpg'
165     };
166     wx.onMenuShareAppMessage(shareData);
167     wx.onMenuShareTimeline(shareData);
168   });
169 
170   wx.error(function (res) {
171     alert(res.errMsg);
172   });
173 </script>
174 </html>
酸番茄 | 初学一级 |园豆:87 | 2015-09-02 00:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册