您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 黄南分类信息网,免费分类信息发布

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

2024/3/8 12:13:33发布18次查看
昨天微信提供了新版本的jssdk,这里修改了一下,支持了3.2版本的命令空间,同时添加了调试模式!
debug = true;    //启用本地调试模式,将官方的两个json文件放到入口文件index.php同级目录即可!
 *       $signpackage = $jssdk->getsignpackage();
 * @命名空间版本
 * @author 阿甘 (qq:33808 624)
 * @date 2015-1-10 14:10
 */
namespace com;
class jssdk {
  private $appid;
  private $appsecret;
  public $debug = false;
public function __construct($appid, $appsecret) {
    $this->appid = $appid;
    $this->appsecret = $appsecret;
  }
public function getsignpackage() {
    $jsapiticket = $this->getjsapiticket();
    $url = http://$_server[http_host]$_server[request_uri];
    $timestamp = time();
    $noncestr = $this->createnoncestr();
// 这里参数的顺序要按照 key 值 ascii 码升序排序
    $string = jsapi_ticket=$jsapiticket&noncestr=$noncestr×tamp=$timestamp&url=$url;
$signature = sha1($string);
$signpackage = array(
      appid     => $this->appid,
      noncestr  => $noncestr,
      timestamp => $timestamp,
      url       => $url,
      signature => $signature,
      rawstring => $string
    );
    return $signpackage; 
  }
private function createnoncestr($length = 16) {
    $chars = abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789;
    $str = ;
    for ($i = 0; $i       $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
  }
private function getjsapiticket() {
      //debug模式
      if ($this->debug) {
        // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
        $data = json_decode(file_get_contents(jsapi_ticket.json));
      } else {
          //从cache中读取,基于thinkphp的缓存机制
          $data = (object)(s('jsapi_ticket_json'));
      }
if ($data->expire_time       $accesstoken = $this->getaccesstoken();
      $url = http://api.weixin.qq.com/cgi-bin/ticket/getticket?type=1&access_token=$accesstoken;
      $res = json_decode($this->httpget($url));
      $ticket = $res->ticket;
if ($ticket) {
        $data->expire_time = time() + 7200;
        $data->jsapi_ticket = $ticket;
//debug模式
        if ($this->debug) {
            $fp = fopen(jsapi_ticket.json, w);
            fwrite($fp, json_encode($data));
            fclose($fp);
        } else {
            //将对象以数组的形式进行缓存
            s('jsapi_ticket_json', (array)$data);
        }
}
    } else {
      $ticket = $data->jsapi_ticket;
    }
return $ticket;
  }
private function getaccesstoken() {
//debug模式
      if ($this->debug) {
        // access_token 应该全局存储与更新,以下代码以写入到文件中做示例
          $data = json_decode(file_get_contents(access_token.json));
          dump($data);
          die();
      } else {
          //从缓存中读取数组并转成对象
        $data = (object)(s('access_token.json'));
      }
if ($data->expire_time       $url = https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appid&secret=$this->appsecret;
      $res = json_decode($this->httpget($url));
      $access_token = $res->access_token;
if ($access_token) {
        $data->expire_time = time() + 7000;
        $data->access_token = $access_token;
//debug模式
        if ($this->debug) {
            $fp = fopen(access_token.json, w);
            fwrite($fp, json_encode($data));
            fclose($fp);
        } else {
            //缓存数组
            s('access_token.json', (array)$data);            
        }
}
    } else {
      $access_token = $data->access_token;
    }
    return $access_token;
  }
private function httpget($url) {
$curl = curl_init();
    curl_setopt($curl, curlopt_returntransfer, true);
    curl_setopt($curl, curlopt_timeout, 500);
    curl_setopt($curl, curlopt_url, $url);
$res = curl_exec($curl);
//错误检测
    $error = curl_error($curl);
curl_close($curl);
//发生错误,抛出异常
    if($error) throw new \exception('请求发生错误(表检查是否在授权域名下访问):' . $error);
return $res;
  }
}
com.rar ( 2.28 kb 下载:202 次 )
ad:真正免费,域名+虚机+企业邮箱=0元
黄南分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录