当前位置:首页 > PHP教程 > php高级应用 > 列表

微信封装的调用微信签名包的类库

发布:smiling 来源: PHP粉丝网  添加日期:2018-09-28 11:15:57 浏览: 评论:0 

废话不多说了,直接给大家贴代码了,具体代码如下所示:

  1. <?php 
  2. namespaceHome\Model; 
  3. useThink\Model; 
  4. classWechatModelextendsModel { 
  5. private$_token=''//令牌 
  6.     private$appid
  7.     private$appsecret
  8.   publicfunction__construct() 
  9.   { 
  10.     $this->appid = C('APPID');//公众号的appid 
  11.     $this->appsecret = C('APPSECRET');//公众号的秘钥 
  12.   } 
  13.   //调用js-sdk的签名包 
  14.   publicfunctiongetSignPackage() { 
  15.   $jsapiTicket=$this->getJsApiTicket(); 
  16.   // 注意 URL 一定要动态获取,不能 hardcode.(获取当前网页的url) 
  17.   $protocol=  (!emptyempty($_SERVER['HTTPS']) &&$_SERVER['HTTPS']  !=='off'||$_SERVER['SERVER_PORT'] == 443) ?"https://":"http://"
  18.   $url="$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"
  19.   //时间戳 
  20.   $timestamp= time(); 
  21.   //随机字符串获取 
  22.   $nonceStr=$this->createNonceStr(); 
  23.   // 这里参数的顺序要按照 key 值 ASCII 码升序排序 
  24.   $string="jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"
  25.   //生成字符串是用来签名用的 
  26.   $signature= sha1($string); 
  27.   $signPackage=array
  28.    "appId"  =>$this->appid, 
  29.    "nonceStr"=>$nonceStr
  30.    "timestamp"=>$timestamp
  31.    "url"   =>$url
  32.    "signature"=>$signature
  33.    "rawString"=>$string 
  34.   ); 
  35.   return$signPackage
  36.  } 
  37.  //使用会员卡领取的签名包 
  38.  publicfunctiongetHuiYuanSignPackage() { 
  39.   $apiTicket=$this->getApiTicket(); 
  40.   // 注意 URL 一定要动态获取,不能 hardcode.(获取当前网页的url) 
  41.   $protocol=  (!emptyempty($_SERVER['HTTPS']) &&$_SERVER['HTTPS']  !=='off'||$_SERVER['SERVER_PORT'] == 443) ?"https://":"http://"
  42.   $url="$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"
  43.   //时间戳 
  44.   $timestamp= time(); 
  45.   //随机字符串获取 
  46.   // $nonceStr = $this->createNonceStr(); 
  47.   // 这里参数的顺序要按照 key 值 ASCII 码升序排序 
  48.   $string=$timestamp.$apiTicket."car_id";//card_id为自己创建的会员卡的id 
  49.   //生成字符串是用来签名用的 
  50.   $signature= sha1($string); 
  51.   $signPackage=array
  52.    "timestamp"=>$timestamp
  53.    "signature"=>$signature
  54.   ); 
  55.   return$signPackage
  56.  } 
  57.  //获取会员卡的api_ticket 
  58.  publicfunctiongetApiTicket(){ 
  59.  $data= json_decode(file_get_contents("api_ticket.json")); 
  60.   if($data->expire_time < time()) { 
  61.    $accessToken=$this->getAccessToken(); 
  62.    $url="https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=wx_card&access_token=$accessToken"
  63.    $res= json_decode($this->httpGet($url)); 
  64.    $ticket=$res->ticket; 
  65.    if($ticket) { 
  66.     $data->expire_time = time() + 7000; 
  67.     $data->jsapi_ticket =$ticket
  68.     $fp=fopen("api_ticket.json","w"); 
  69.     fwrite($fp, json_encode($data)); 
  70.     fclose($fp); 
  71.    } 
  72.   }else
  73.    $ticket=$data->jsapi_ticket; 
  74.   } 
  75.   return$ticket
  76.  } 
  77.  //获取随机字符串 
  78.  privatefunctioncreateNonceStr($length= 16) { 
  79.   $chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  80.   $str=""
  81.   for($i= 0;$i<$length;$i++) { 
  82.    $str.=substr($chars, mt_rand(0,strlen($chars) - 1), 1); 
  83.   } 
  84.   return$str
  85.  } 
  86.   //获取Access Token 
  87.   publicfunctiongetAccessToken(){ 
  88.   //将json字符串转换为json对象(json_encode是将数组转换为json字符串,json_decode("",true) 如果加true是将json字符串转化为php数组,不加true转换为PHP对象) 
  89.   $data= json_decode(file_get_contents("access_token.json")); 
  90.   if($data->expire_time < time()) { 
  91.    // 如果是企业号用以下URL获取access_token 
  92.    $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appid&secret=$this->appsecret"
  93.    $res= json_decode($this->httpGet($url)); 
  94.    $access_token=$res->access_token; 
  95.  if($access_token) { 
  96.     $data->expire_time = time() + 7000; 
  97.     $data->access_token =$access_token
  98.     $fp=fopen("access_token.json","w"); 
  99.     fwrite($fp, json_encode($data)); 
  100.     fclose($fp); 
  101.  } 
  102.   }else
  103.    $access_token=$data->access_token; 
  104.   } 
  105.   return$access_token
  106.   } 
  107.  //获取jsapi_ticket(jsapi_ticket是公众号用于调用微信JS接口的临时票据) 
  108.   privatefunctiongetJsApiTicket() { 
  109.   // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例 
  110.   $data= json_decode(file_get_contents("jsapi_ticket.json")); 
  111.   if($data->expire_time < time()) { 
  112.    $accessToken=$this->getAccessToken(); 
  113.    // 如果是企业号用以下 URL 获取 ticket 
  114.    // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; 
  115.    $url="https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"
  116.    $res= json_decode($this->httpGet($url)); 
  117.    $ticket=$res->ticket; 
  118.    if($ticket) { 
  119.     $data->expire_time = time() + 7000; 
  120.     $data->jsapi_ticket =$ticket
  121.     $fp=fopen("jsapi_ticket.json","w"); 
  122.     fwrite($fp, json_encode($data)); 
  123.     fclose($fp); 
  124.    } 
  125.   }else
  126.    $ticket=$data->jsapi_ticket; 
  127.   } 
  128.   return$ticket
  129.  } 
  130.   //获取用户的openid 
  131.   publicfunctionopenId(){ 
  132.   $url=$_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI']; 
  133.     if(!isset($_GET['code'])) { 
  134.      //获取组装的url 
  135.       $openidUrl=$this->snsapi_base($url); 
  136.       redirect($openidUrl); 
  137.     }else
  138.       $openidAccess_token=$this->openidAccess_token($_GET['code']); 
  139.       return$openidAccess_token
  140.     } 
  141.   } 
  142.    //获取微信用户的opnid 
  143.   publicfunctiongetOpenId($openid,$access_token
  144.   { 
  145.     $userInfo=$this->getUserInfo($openid,$access_token); 
  146.     return$userInfo
  147.   } 
  148.    publicfunctionsnsapi_base($redirect_uri,$scope="snsapi_userinfo",$state= 0) 
  149.   { 
  150.     $appId=$this->appid; 
  151.     $url="https://open.weixin.qq.com/connect/oauth2/authorize"
  152.     $url.="?appid=$appId"
  153.     $url.="&redirect_uri=http://$redirect_uri"
  154.     $url.="&response_type=code"
  155.     $url.="&scope=$scope"
  156.     $url.="&state=$state#wechat_redirect"
  157.     return$url
  158.   } 
  159. publicfunctionopenidAccess_token($code){ 
  160.     $appId=$this->appid; 
  161.     $appSecret=$this->appsecret; 
  162.     $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appId&secret=$appSecret&code=$code&grant_type=authorization_code"
  163.     returnjson_decode($this->httpGet($url),true); 
  164.   } 
  165.   //获取用户信息 
  166.   publicfunctiongetUserInfo($openid,$access_token){ 
  167.   $url="https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN "
  168.     returnjson_decode($this->httpGet($url),true); 
  169.    //请求 
  170.   } 
  171. privatefunctionhttpGet($url) { 
  172.   $curl= curl_init(); 
  173.   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
  174.   curl_setopt($curl, CURLOPT_TIMEOUT, 500); 
  175.   curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
  176.   curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 
  177.   curl_setopt($curl, CURLOPT_URL,$url); 
  178.   $res= curl_exec($curl); 
  179.   curl_close($curl); 
  180.   return$res
  181.  } 

Tags: 微信封装 微信签名 类库

分享到: