微信封装的调用微信签名包的类库
程序员文章站
2024-03-09 16:26:41
废话不多说了,直接给大家贴代码了,具体代码如下所示:
废话不多说了,直接给大家贴代码了,具体代码如下所示:
<?php namespace home\model; use think\model; class wechatmodel extends model { private $_token = ''; //令牌 private $appid; private $appsecret; public function __construct() { $this->appid = c('appid');//公众号的appid $this->appsecret = c('appsecret');//公众号的秘钥 } //调用js-sdk的签名包 public function getsignpackage() { $jsapiticket = $this->getjsapiticket(); // 注意 url 一定要动态获取,不能 hardcode.(获取当前网页的url) $protocol = (!empty($_server['https']) && $_server['https'] !== 'off' || $_server['server_port'] == 443) ? "https://" : "http://"; $url = "$protocol$_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; } //使用会员卡领取的签名包 public function gethuiyuansignpackage() { $apiticket = $this->getapiticket(); // 注意 url 一定要动态获取,不能 hardcode.(获取当前网页的url) $protocol = (!empty($_server['https']) && $_server['https'] !== 'off' || $_server['server_port'] == 443) ? "https://" : "http://"; $url = "$protocol$_server[http_host]$_server[request_uri]"; //时间戳 $timestamp = time(); //随机字符串获取 // $noncestr = $this->createnoncestr(); // 这里参数的顺序要按照 key 值 ascii 码升序排序 $string = $timestamp.$apiticket."car_id";//card_id为自己创建的会员卡的id //生成字符串是用来签名用的 $signature = sha1($string); $signpackage = array( "timestamp" => $timestamp, "signature" => $signature, ); return $signpackage; } //获取会员卡的api_ticket public function getapiticket(){ $data = json_decode(file_get_contents("api_ticket.json")); if ($data->expire_time < time()) { $accesstoken = $this->getaccesstoken(); $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=wx_card&access_token=$accesstoken"; $res = json_decode($this->httpget($url)); $ticket = $res->ticket; if ($ticket) { $data->expire_time = time() + 7000; $data->jsapi_ticket = $ticket; $fp = fopen("api_ticket.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $ticket = $data->jsapi_ticket; } return $ticket; } //获取随机字符串 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; } //获取access token public function getaccesstoken(){ //将json字符串转换为json对象(json_encode是将数组转换为json字符串,json_decode("",true) 如果加true是将json字符串转化为php数组,不加true转换为php对象) $data = json_decode(file_get_contents("access_token.json")); if ($data->expire_time < time()) { // 如果是企业号用以下url获取access_token $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; $fp = fopen("access_token.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $access_token = $data->access_token; } return $access_token; } //获取jsapi_ticket(jsapi_ticket是公众号用于调用微信js接口的临时票据) private function getjsapiticket() { // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例 $data = json_decode(file_get_contents("jsapi_ticket.json")); if ($data->expire_time < time()) { $accesstoken = $this->getaccesstoken(); // 如果是企业号用以下 url 获取 ticket // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accesstoken"; $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accesstoken"; $res = json_decode($this->httpget($url)); $ticket = $res->ticket; if ($ticket) { $data->expire_time = time() + 7000; $data->jsapi_ticket = $ticket; $fp = fopen("jsapi_ticket.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $ticket = $data->jsapi_ticket; } return $ticket; } //获取用户的openid public function openid(){ $url = $_server['http_host'] . $_server['request_uri']; if (!isset($_get['code'])) { //获取组装的url $openidurl = $this->snsapi_base($url); redirect($openidurl); }else{ $openidaccess_token = $this->openidaccess_token($_get['code']); return $openidaccess_token; } } //获取微信用户的opnid public function getopenid($openid,$access_token) { $userinfo = $this->getuserinfo($openid,$access_token); return $userinfo; } public function snsapi_base($redirect_uri, $scope = "snsapi_userinfo", $state = 0) { $appid = $this->appid; $url = "https://open.weixin.qq.com/connect/oauth2/authorize"; $url .= "?appid=$appid"; $url .= "&redirect_uri=http://$redirect_uri"; $url .= "&response_type=code"; $url .= "&scope=$scope"; $url .= "&state=$state#wechat_redirect"; return $url; } public function openidaccess_token($code){ $appid = $this->appid; $appsecret= $this->appsecret; $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code"; return json_decode($this->httpget($url),true); } //获取用户信息 public function getuserinfo($openid, $access_token){ $url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_cn "; return json_decode($this->httpget($url),true); //请求 } private function httpget($url) { $curl = curl_init(); curl_setopt($curl, curlopt_returntransfer, true); curl_setopt($curl, curlopt_timeout, 500); curl_setopt($curl, curlopt_ssl_verifypeer, false); curl_setopt($curl, curlopt_ssl_verifyhost, false); curl_setopt($curl, curlopt_url, $url); $res = curl_exec($curl); curl_close($curl); return $res; } }
以上所述是小编给大家介绍的微信封装的调用微信签名包的类库,希望对大家有所帮助
上一篇: SqlDataSource 链接Access 数据
下一篇: Namp的使用