1 <?php 2 namespace Home\Controller; 3 use Think\Controller; 4 class IndexController extends Controller { 5 public function index(){ 6 //获得参数 signature nonce token timestamp echostr 7 $timestamp=$_GET[\'timestamp\']; 8 $nonce=$_GET[\'nonce\']; 9 $token=\'weixin\'; 10 $signature=$_GET[\'signature\']; 11 $echostr = $_GET[\'echostr\']; 12 //形成数组,然后按字典序排序 13 $array=array($timestamp,$nonce,$token); 14 sort($array); 15 //拼接成字符串,加密,然后与signature进行校验 16 $str = sha1( implode( $array ) ); 17 if($str == $signature && $echostr){ 18 header(\'content-type:text\');//新浪SAE没有实名验证加了这一句 19 //第一次接入weixin api接口的时候,需要验证,第二次就不用了 20 echo $echostr; 21 exit; 22 }else{ 23 $this->reponseMsg(); 24 } 25 26 } 27 28 public function reponseMsg(){ 29 //1.获取到微信推送过来post数据(xml格式) 30 $postArr=$GLOBALS[\'HTTP_RAW_POST_DATA\']; 31 //2.处理消息类型,并设置回复类型和内容 32 //打开微信开发者文档-消息管理-接收事件文档 33 //用此函数将xml标签转换为对象 34 $postObj=simplexml_load_string($postArr); 35 //判断该数据包是否是订阅的事件推送 36 if(strtolower($postObj->MsgType)==\'event\'){ 37 //如果是订阅 subscribe 事件 38 if(strtolower($postObj->Event)==\'subscribe\'){ 39 //回复用户消息(单图文) 40 $arr=array( 41 array( 42 \'title\'=>\'欢迎关注我的读书笔记!\', 43 \'description\'=>"千江有水千江月,万里无云万里天", 44 \'picUrl\'=>\'http://l1230205.applinzi.com/gzh/Wx/image/timg.jpg\', 45 \'url\'=>\'http://www.hao123.com\', 46 ), 47 ); 48 //实例化模型 49 $indexModel=new \Home\Model\IndexModel; 50 $indexModel->responseSubscribe($postObj,$arr); 51 } 52 53 //如果是重扫二维码 54 if(strtolower($postObj->Event)==\'scan\'){ 55 if($postObj->EventKey == 2000){ 56 //如果是临时二维码扫码 57 $tmp=\'临时二维码欢迎你\'; 58 }//2000为临时二维码代码中设置的参数 59 if($postObj->EventKey == 3000){ 60 //如果是永久二维码扫码 61 $tmp=\'永久二维码欢迎你\'; 62 }//3000为临时二维码代码中设置的参数 63 $arr=array( 64 array( 65 \'title\'=>$tmp, 66 \'description\'=>"千江有水千江月,万里无云万里天", 67 \'picUrl\'=>\'http://l1230205.applinzi.com/gzh/Wx/image/timg.jpg\', 68 \'url\'=>\'http://www.hao123.com\', 69 ), 70 ); 71 //实例化模型 72 $indexModel=new \Home\Model\IndexModel; 73 $indexModel->responseSubscribe($postObj,$arr); 74 75 } ////如果是重扫二维码结束 76 77 78 79 //点击菜单拉取消息时的事件推送开始,下面回复为单文本模式 80 if(strtolower($postObj->Event)==\'click\'){ 81 //如果是自定义中菜单中的event->click 82 if(strtolower($postObj->EventKey)==\'item1\'){ 83 $content="这是item1菜单的事件推送"; 84 } 85 if(strtolower($postObj->EventKey)==\'songs\'){ 86 $content="这是歌曲菜单的事件推送"; 87 } 88 //实例化模型 89 $indexModel=new \Home\Model\IndexModel; 90 $indexModel->responseText($postObj,$content); 91 92 } 93 //点击菜单拉取消息时的事件推送,跳转的 94 if(strtolower($postObj->Event)==\'view\'){ 95 //如果是自定义中菜单中的event->view 96 $content="跳转的链接是".$postObj->EventKey; 97 //因为页面直接跳转所以没有推送 98 //实例化模型 99 $indexModel=new \Home\Model\IndexModel; 100 $indexModel->responseText($postObj,$content); 101 102 }//点击菜单拉取消息时的事件推送结束 103 } 104 //用户发送tuwen2关键字的时候,回复多个单图文 105 //注意:进行多图文发送时,子图文个数不能超过10个 106 if( strtolower($postObj->MsgType) == \'text\' && trim($postObj->Content)==\'tuwen2\'){ 107 //从数据库中得到 108 $arr=array( 109 array( 110 \'title\'=>\'百度一下哟!\', 111 \'description\'=>"有什么问题你尽管问百度,它啥都懂", 112 \'picUrl\'=>\'https://www.baidu.com/img/bdlogo.png\', 113 \'url\'=>\'http://www.hao123.com\', 114 ), 115 array( 116 \'title\'=>\'幕课网程序员的梦工厂\', 117 \'description\'=>"幕课网程序员的梦工厂", 118 \'picUrl\'=>\'https://www.baidu.com/img/bdlogo.png\', 119 \'url\'=>\'http://www.imooc.com\', 120 ), 121 array( 122 \'title\'=>\'给我你的QQ号\', 123 \'description\'=>"你QQ号多少啊?", 124 \'picUrl\'=>\'https://www.baidu.com/img/bdlogo.png\', 125 \'url\'=>\'http://www.qq.com\', 126 ), 127 ); 128 //实例化模型 129 $indexModel=new \Home\Model\IndexModel; 130 $indexModel->responseNews($postObj,$arr); 131 }else{ 132 //trim是为了防止输入误打的时候不匹配 133 //关键字回复 134 switch( trim($postObj->Content) ){ 135 case 1: 136 $content=\'您输入的数字是1\'; 137 break; 138 case 2: 139 $content=\'您输入的数字是2\'; 140 break; 141 case 3: 142 $content=\'您输入的数字是3\'; 143 break; 144 case \'有问题\': 145 //在回复的文本中,投放超链接 146 $content="<a href=\'http://www.baidu.com\'>有问题你找百度啊!</a>"; 147 break; 148 case \'好想你\': 149 $content=\'你还是想别人吧\'; 150 break; 151 default: 152 $content=\'还在测试中......\'; 153 break; 154 case \'天气\': 155 //心知天气API接口调用 156 $ch=curl_init(); 157 $city=\'沈阳\'; 158 $url=\'https://api.seniverse.com/v3/weather/now.json?key=cgc69plib0upjlfa&language=zh-Hans&unit=c&location=\'.urlencode($city); 159 160 //3.设置参数 161 curl_setopt($ch,CURLOPT_URL,$url); 162 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 163 $res=curl_exec($ch); 164 if(curl_errno($ch)){ 165 var_dump(curl_error($ch)); 166 } 167 //5.关闭 168 curl_close($ch); 169 //如果出现错误,打印错误 170 $arr=json_decode($res,true); 171 $content=$city.\'的天气是:\'.$arr[\'results\'][\'0\'][\'now\'][\'text\'].\',气温是\'.$arr[\'results\'][\'0\'][\'now\'][\'temperature\']; 172 //心知天气API接口调用结束 173 break; 174 175 176 } 177 178 //实例化模型 179 $indexModel=new \Home\Model\IndexModel; 180 $indexModel->responseText($postObj,$content); 181 182 }//if end 183 184 } // responseMsg end 185 186 187 /* 188 $url 接口的url 189 $type 请求类型 190 $res 返回数据类型 191 $arr post请求参数 192 193 */ 194 //curl采集器 195 public function http_curl($url,$type=\'get\',$res=\'json\',$arr=\'\'){ 196 //1.初始化curl 197 $ch=curl_init(); 198 //2.设置curl的参数 199 curl_setopt($ch,CURLOPT_URL,$url); 200 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 201 if($type==\'post\'){ 202 curl_setopt($ch,CURLOPT_POST,1); 203 curl_setopt($ch,CURLOPT_POSTFIELDS,$arr); 204 } 205 //3.采集 206 $output=curl_exec($ch); 207 //4.关闭 208 curl_close($ch); 209 //如果返回的值,是json格式,则转换成数组 210 if($res==\'json\'){ 211 if(curl_errno($ch)){ 212 //请求失败,返回错误信息 213 return curl_error($ch); 214 }else{ 215 //请求成功 216 return json_decode($output,true); 217 } 218 } 219 }//http_curl end 220 221 222 223 224 225 //获取ACCESS_TOKEN 226 function getWxAccessToken01(){ 227 //1.请求url地址 228 //$appid=\'wxa8f829e8827fd059\'; 229 //$appsecret=\'92faffeb1f85c8240713a222969c8d50\'; 230 //换成测试账号的 231 $appid=\'wx4bba2f41aab513ef\'; 232 $appsecret=\'ce622479d17be1b6bc25edd86ffdf43f\'; 233 234 $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret; 235 //2.初始化 236 $ch=curl_init; 237 //3.设置参数 238 curl_setopt($ch,CURLOPT_URL,$url); 239 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 240 //4.调用接口 241 $res=curl_exec($ch); 242 if(curl_errno($ch)){ 243 var_dump(curl_error($ch)); 244 } 245 246 //5.关闭 247 curl_close($ch); 248 //如果出现错误,打印错误 249 250 $arr=json_decode($res,true); 251 var_dump($arr); 252 253 }//getWxAccessToken end 254 //新浪SAE上传此段代码在不报错的情况下返回NULL,最后通过在线接口调试工具,获取了access_token 255 256 257 258 //获取微信服务器地址IP 259 function getWxServerIp(){ 260 $accessToken="11_iDDcEkGcHnwvBmaFbkFd-vPgBNWTcfcByJpagSbClKKQJAGeq5ruVFUibBPWj0n1WzY68asfOqzoSbJGaWa3f-2hv_QgS0rNF2ItduaOFLFTjl-e6r6Ob3s9y-ANWMdAEAQZP"; 261 $url = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".$accessToken; 262 $ch = curl_init(); 263 curl_setopt($ch, CURLOPT_URL,$url); 264 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 265 $res = curl_exec($ch); 266 curl_close($ch); 267 if(curl_errno($ch)){ 268 var_dump(curl_error($ch)); 269 } 270 $arr = json_decode($res,true); 271 echo "<pre>"; 272 var_dump( $arr ); 273 echo "</pre>"; 274 } //getWxServerIp end; 275 276 277 278 279 280 /*//封装的post方法的curl 281 function curl_post($url, $arr_data){ 282 $post_data = http_build_query($url_data); 283 $ch = curl_init(); 284 curl_setopt($ch, CURLOPT_URL, $url); 285 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 286 curl_setopt($ch, CURLOPT_POST, 1); 287 curl_setopt($ch, CURLOPT_POSTFLELDS, $post_data); 288 $data = curl_exec($ch); 289 curl_close($ch); 290 echo $data; 291 } 292 293 */ 294 295 //微信群发接口开发 296 function sendMsgAll(){ 297 //1.获取全局access_token 298 $access_token=\'12_p6aGdDKhtDbb5Reww-L8N7yYMJaMzr_mRajDf3VWPdYIkphJk_Kz8jUOWQTZdth0vMIdi2E971EjmsBgIcjnUUDfw-KKPKeyHTcXhEerCJ_IeVuAqh0rrT1x0FIJW1i0oOK1Ws7tuZMeYai7GPFiAIAKTJ\'; 299 $url="https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=".$access_token; 300 //返回Null接口未获权限 301 302 //2.组装群发接口数据array 303 //单文本 //多图文失败了,因为media_id不对 304 $array=array( 305 \'touser\'=>\'oYimv0dNGcVxYrr_noub4cavTgF0\', //微信用户的openID,来自于测试账号里面的微信号 306 \'text\'=>array(\'content\'=>\'you are very good!\'),//文本内容 307 \'msgtype\'=>\'text\', 308 ); 309 310 //3.将array->json 311 $postJson=json_encode($array); 312 var_dump($postJson); 313 echo "<hr/>"; 314 //4.curl 315 $res=$this->http_curl($url,$type=\'post\',$res=\'json\',$postJson); 316 var_dump($res); 317 } //sendMsgAll end 318 319 //【目前测试没有成功的一种写法】 320 //返回存储调用的$access_token的方法【重要的】 ------返回空白 321 public function getWxAccessToken(){ 322 //将access_token 存在session或者cookie中 323 if($_SESSION[\'access_token\'] && $_SESSION[\'expire_time\']>time()){ 324 //如果access_token在session并没有过期 325 return $_SESSION[\'access_token\']; 326 327 }else{ 328 //如果access_token不存在或者已经过期,重新取 329 //换成测试账号的 330 $appid=\'wx4bba2f41aab513ef\'; 331 $appsecret=\'ce622479d17be1b6bc25edd86ffdf43f\'; 332 $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret; 333 $res=$this->http_curl($url,\'get\',\'json\'); 334 $access_token=$res[\'access_token\']; 335 //将重新获取到的access_token存到session 336 $_SESSION[\'access_token\']=$access_token; 337 //设置过期时间 338 $_SESSION[\'expire_time\']=time()+7200; 339 return $access_token; 340 } 341 }//getWxAccessToken end 342 343 344 345 //微信菜单管理接口 346 Public function definedItem(){ 347 header(\'content-type:text/html; charset=utf-8\'); 348 //创建微信菜单 349 //目前微信接口的调用方式都是通过curl post/get 350 $access_token=\'12_p6aGdDKhtDbb5Reww-L8N7yYMJaMzr_mRajDf3VWPdYIkphJk_Kz8jUOWQTZdth0vMIdi2E971EjmsBgIcjnUUDfw-KKPKeyHTcXhEerCJ_IeVuAqh0rrT1x0FIJW1i0oOK1Ws7tuZMeYai7GPFiAIAKTJ\'; 351 $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token; 352 $postArr=array( 353 \'button\'=>array( 354 array( 355 \'name\'=>urlencode(\'菜单一\'),//urlencode为了正确的编码格式 356 \'type\'=>\'click\', 357 \'key\'=>\'item1\', 358 ),//第一个一级菜单 359 array( 360 \'name\'=>urlencode(\'菜单二\'), 361 \'sub_button\'=>array( 362 array( 363 \'name\'=>urlencode(\'歌曲\'), 364 \'type\'=>\'click\', 365 \'key\'=>\'songs\', 366 ),//第一个二级菜单 367 array( 368 \'name\'=>urlencode(\'电影\'), 369 \'type\'=>\'view\', 370 \'url\'=>\'http://www.baidu.com\', 371 ),//第二个二级菜单 372 ), 373 ),//第二个一级菜单 374 array( 375 \'name\'=>urlencode(\'菜单三\'), 376 \'type\'=>\'view\', 377 \'url\'=>\'http://www.qq.com\', 378 )//第三个一级菜单), 379 ), 380 381 ); 382 $postJson=urldecode(json_encode($postArr));//反转回来 383 $res=$this->http_curl($url,\'post\',\'json\',$postJson); 384 var_dump($res); 385 386 }//definedItem end 387 388 //模板消息接口,测试账号做,一般消费之后的推送 389 function sendTemplateMsg(){ 390 //可以申请到15个模板 391 //一个账号的当日的模板消息次数不超过10万次 392 //1.获取access_token 393 $access_token=\'12__LIFZkJPeWj1Dvfn_GsT0Gb19WMBZZgdYCmG5Yxh6VpWxdPgtevmH2b5nayY1i3AjHgp3P5VuyM07H3YN0xudlt0R6I6n8bKcIeuX5OkqBsdURpAkAPVTr7R-z1UXZ2ERCY9WbVu9dzFQOIbCQOgAIAPDB\'; 394 $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;//模板跳转链接 395 //2.组装数组 396 $array=array( 397 \'touser\'=>\'oYimv0dNGcVxYrr_noub4cavTgF0\', 398 \'template_id\'=>\'LGHacJgQ4RepAwGLfaQ-BwFt_pYDvJxMzQrwCJJHlcQ\',//测试号里生成的模板id 399 \'url\'=>\'http://www.qq.com\', 400 \'data\'=>array( 401 \'name\'=>array(\'value\'=>\'云散明月谁点缀?天容海色本澄清\',\'color\'=>"#C71585"), 402 \'money\'=>array(\'value\'=>1000,\'color\'=>"#C71585"), 403 \'date\'=>array(\'value\'=>date(\'Y-m-d H:i:s\'),\'color\'=>"#C71585") 404 ), 405 ); 406 //3.将数组转换成json 407 $postJson=json_encode($array); 408 //4.调用curl函数 409 $res=$this->http_curl($url,\'post\',\'json\',$postJson); 410 var_dump($res); 411 412 } //sendTemplateMsg end 413 //网页授权接口 // 并未实现,报错40163 414 //获取用户的的openid 415 function getBaseInfo(){ 416 //1.获取到code 417 $appid="wx4bba2f41aab513ef"; 418 $redirect_uri=urlencode("http://l1230205.applinzi.com/gzh/wx.php/Index/getUserOpenId"); 419 //必须和测试号网页授权获取用户基本信息里面的修改中的地址一致,否则会报错 420 $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=123#wechat_redirect"; 421 //跳转至getUserOpenId() 422 header(\'Location:\'.$url);exit; 423 424 425 }//getBaseInfo end 426 427 function getUserOpenId(){ 428 //2.获取到网页授权的access_token,不同于上面获取的全局access_token 429 $appid="wx4bba2f41aab513ef"; 430 $appsecret="ce622479d17be1b6bc25edd86ffdf43f"; 431 $code=$_GET[\'code\']; 432 $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code"; 433 //3.获取用户的openid 434 $res=$this->http_curl($url,\'get\'); 435 var_dump($res); 436 $openid=$res[\'openid\']; 437 //time(); 438 //1,2,3 假如允许用户一天参加3次,就给保存登陆的时间,次数 439 //$this->display(\'index.tpl\');//参加活动的页面 440 //菜单栏中提供的网址http://l1230205.applinzi.com/gzh/wx.php/Index/getBaseInfo 441 442 } //getUserOpenId end 443 444 //获取用户的详细授权信息 445 function getUserDetail(){ 446 //1.获取到code 447 $appid="wx4bba2f41aab513ef"; 448 $redirect_uri=urlencode("http://l1230205.applinzi.com/gzh/wx.php/Index/getUserInfo"); 449 //必须和测试号网页授权获取用户基本信息里面的修改中的地址一致,否则会报错l1230205.applinzi.com 450 $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"; 451 //跳转至getUserOpenId() 452 header(\'Location:\'.$url);exit; 453 454 }//getUserDetail end 455 456 function getUserInfo(){ 457 //2.获取到网页授权的access_token 458 $appid="wx4bba2f41aab513ef"; 459 $appsecret="ce622479d17be1b6bc25edd86ffdf43f"; 460 $code=$_GET[\'code\']; 461 $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code"; 462 //3.获取用户的openid 463 $res=$this->http_curl($url,\'get\'); 464 $access_token=$res[\'access_token\']; 465 $openid=$res[\'openid\']; 466 //拉取用户的详细信息 467 $url="https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN"; 468 $res=$this->http_curl($url); 469 var_dump($res); 470 471 472 }//getUserInfo end 473 //生成带参数的临时二维码 474 function getTimeQrCode(){ 475 header(\'content-type:text/html;charset=utf-8\'); 476 //1.获取ticket票据 477 //2.全局票据access_token网页授权access_token 威信js-SDK jsapi_ticket临时的这三个都有过期时间 478 $access_token="12_EDjuJXo7AKFzpxOWcgis-F_EaXNT6ITQt6ZBbMgMUhuqySF-5aD5bUv-p1TPT63oTIy3uXSDVMDIbnrcpzdFXgfqxHGZbN2_-lzlZ8MKUQ3dskH3B7wpkr1aQymY459MjXYfioZJJW7FNYnLYMNjAHAIWX"; 479 $url="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token; 480 $postArr=array( 481 \'expire_seconds\'=>604800,//24*60*60 482 \'action_name\'=>"QR_SCENE", 483 \'action_info\'=>array( 484 \'scene\'=>array(\'scene_id\'=>2000), 485 ), 486 ); 487 $postJson=json_encode($postArr); 488 $res=$this->http_curl($url,\'post\',\'json\',$postJson); 489 $ticket=$res[\'ticket\']; 490 //2.使用ticket获取二维码图片 491 $url="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket); 492 //上面二维码的地址已经生成了,直接展示 493 echo \'临时二维码\'; 494 echo "<img src=\'".$url."\' />"; 495 }//getQrCode end 496 497 //生成带参数的永久二维码 498 function getForeverQrCode(){ 499 header(\'content-type:text/html;charset=utf-8\'); 500 //1.获取ticket票据 501 //2.全局票据access_token网页授权access_token 微信js-SDK jsapi_ticket临时的这三个都有过期时间 502 $access_token="12_EDjuJXo7AKFzpxOWcgis-F_EaXNT6ITQt6ZBbMgMUhuqySF-5aD5bUv-p1TPT63oTIy3uXSDVMDIbnrcpzdFXgfqxHGZbN2_-lzlZ8MKUQ3dskH3B7wpkr1aQymY459MjXYfioZJJW7FNYnLYMNjAHAIWX"; 503 $url="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token; 504 $postArr=array( 505 \'action_name\'=>"QR_LIMIT_SCENE", 506 \'action_info\'=>array( 507 \'scene\'=>array(\'scene_id\'=>3000), 508 ), 509 ); 510 $postJson=json_encode($postArr); 511 $res=$this->http_curl($url,\'post\',\'json\',$postJson); 512 $ticket=$res[\'ticket\']; 513 //2.使用ticket获取二维码图片 514 $url="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket); 515 //上面二维码的地址已经生成了,直接展示 516 echo \'永久二维码\'; 517 echo "<img src=\'".$url."\' />"; 518 }//getQrCode end 519 //获取JsApiTicket票据的方法 520 function getJsApiTicket(){ 521 //如果session中保存 522 if($_SESSION[\'jsapi_ticket_expire_time\']>time() && $_SESSION[\'jsapi_ticket\']){ 523 $jsapi_ticket=$_SESSION[\'jsapi_ticket\']; 524 }else{ 525 //$access_token=$this->getWxAccessToken();因为获取失败 不好用 所以测试账号获得 526 $access_token="12_EDjuJXo7AKFzpxOWcgis-F_EaXNT6ITQt6ZBbMgMUhuqySF-5aD5bUv-p1TPT63oTIy3uXSDVMDIbnrcpzdFXgfqxHGZbN2_-lzlZ8MKUQ3dskH3B7wpkr1aQymY459MjXYfioZJJW7FNYnLYMNjAHAIWX"; 527 $url="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi"; 528 $res=$this->http_curl($url); 529 $jsapi_ticket=$res[\'ticket\']; 530 $_SESSION[\'jsapi_ticket\']=$jsapi_ticket; 531 $_SESSION[\'jsapi_ticket_expire_time\']=time()+7000; 532 } 533 return $jsapi_ticket; 534 }//getJsApiTicket end 535 //获取noncestr随机码 536 function getRandCode($num=16){ 537 $array=array( 538 \'A\',\'B\',\'C\',\'D\',\'E\',\'F\',\'G\',\'H\',\'I\',\'J\',\'K\',\'L\',\'M\',\'N\',\'O\',\'P\',\'Q\',\'R\',\'S\',\'T\',\'U\',\'V\',\'W\',\'X\',\'Y\',\'Z\', 539 \'a\',\'b\',\'c\',\'d\',\'e\',\'f\',\'g\',\'h\',\'i\',\'j\',\'k\',\'l\',\'m\',\'n\',\'o\',\'p\',\'q\',\'r\',\'s\',\'t\',\'u\',\'v\',\'w\',\'x\',\'y\',\'z\', 540 \'0\',\'1\',\'2\',\'3\',\'4\',\'5\',\'6\',\'7\',\'8\',\'9\' 541 ); 542 $tmpstr=\'\'; 543 $max=count($array); 544 for($i=1;$i<=$num;$i++){ 545 $key=rand(0,$max-1); 546 $tmpstr.=$array[$key]; 547 } 548 return $tmpstr; 549 550 551 } //getRandCode end 552 //微信分享 553 function shareWx(){ 554 //1.获取jsapi_ticket票据 555 $jsapi_ticket=$this->getJsApiTicket(); 556 $timestamp=time(); 557 $noncestr=$this->getRandCode(); 558 $url=\'http://l1230205.applinzi.com/gzh/wx.php/Index/shareWx\'; 559 //2.获取signature 560 $signature="jsapi_ticket=".$jsapi_ticket."&noncestr=".$noncestr."×tamp=".$timestamp."&url=".$url; 561 $signature=sha1($signature); 562 $this->assign(\'name\',\'imooc\'); 563 $this->assign(\'timestamp\',$timestamp); 564 $this->assign(\'noncestr\',$noncestr); 565 $this->assign(\'signature\',$signature); 566 $this->display(\'share\'); 567 }//shareWx end 568 569 570 571 }//calss end
以上代码是【/gzh/Wx/Home/Controller/IndexController.class.php】页面内容