123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- class JSSDK1{
-
- public $appId;
-
- private $appSecret;
-
- public function __construct( $appId , $appSecret ) {
- $this -> appId = $appId;
- $this -> appSecret = $appSecret;
- }
-
- public function getAcc(){
- return $this -> getAccessToken();
- }
-
- public function getWxConfig() {
-
- $jsapiTicket = $this -> getJsApiTicket();
-
- $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
- $url = 'https://iot.usky.cn/jdxf/wxapp4.php';
-
- $timestamp = time();
-
- $nonceStr = $this -> createNonceStr();
-
-
- $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
-
- $signature = sha1($string);
-
- $WxConfig = array(
- "appId" => $this -> appId,
- "nonceStr" => $nonceStr,
- "timestamp" => $timestamp,
- "url" => $url,
- "signature" => $signature,
- "rawString" => $string
- );
-
- return $WxConfig;
- }
-
- private function getJsApiTicket() {
-
- $ticket = S('ticket');
-
- $accessToken = $this->getAccessToken();
-
-
- $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
-
- $result = json_decode($this->httpGet($url),true);
- $ticket = $result['ticket'];
-
- if ($ticket) {
-
-
-
-
-
-
-
- S('ticket',$ticket,$result['expires_in']);
- }
-
- return $ticket;
- }
-
- private function getAccessToken() {
-
- $access_token = S('accesToken');
-
- $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
-
- $result = json_decode($this->httpGet($url),true);
- $access_token = $result['access_token'];
-
- if ($access_token) {
- S('accesToken',$result['access_token'],$result['expires_in']);
- }
-
- return $access_token;
- }
-
- private function httpGet($url) {
-
- $curl = curl_init();
-
-
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_TIMEOUT, 500);
- curl_setopt($curl, CURLOPT_URL, $url);
-
- $res = curl_exec($curl);
-
- curl_close($curl);
-
- return $res;
- }
-
- private function createNonceStr($length = 16) {
- $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- $str = "";
- for ($i = 0; $i < $length; $i++) {
- $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
- }
- return $str;
- }
- }
|