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

本篇小说首要为大家介绍了微信封装的调用Wechat具名包的类库的有关质地,很有参照他事他说加以考查价值,有须要的恋人就伙同参谋一下呢。

废话十分少说了,直接给我们贴代码了,具体代码如下所示:

<?php
class JSSDK {
private $appId;//appid
private $appSecret;//应用密钥
// echo $appId;
// echo $appSecret;
public function __construct($appId, $appSecret) {
$this->appId = $appId;
$this->appSecret = $appSecret;
}

废话相当的少说了,直接给我们贴代码了,具体代码如下所示:

 

public function getSignPackage() {
$jsapiTicket = $this->getJsApiTicket();
$url = “]”;
$timestamp = time();
$nonceStr = $this->createNonceStr();

代码如下复制代码

 代码如下

// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";

$signature = sha1($string);

$signPackage = array(
  "appId"     => $this->appId,
  "nonceStr"  => $nonceStr,
  "timestamp" => $timestamp,
  "url"       => $url,
  "signature" => $signature,
  "rawString" => $string
);
return $signPackage; 

?php

<?php

}

namespaceHome/Model;

namespaceHomeModel;

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;
}

useThink/Model;

useThinkModel;

public function getJsApiTicket() {
// jsapi_ticket 应该全局存款和储蓄与更新,以下代码以写入到文件中做示范
$accessToken = $this->getAccessToken();
$url =
“https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken”;
$res = json_decode($this->httpGet($url));
$ticket = $res->ticket;
return $ticket;
}

classWechatModelextendsModel {

classWechatModelextendsModel {

public function getAccessToken() {
// 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;
return $access_token;
}
function httpGet($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “POST”);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, ‘Mozilla/5.0 (compatible; MSIE
5.01; Windows NT 5.0)’);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
// curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

private$_token=//令牌

private$_token=”//令牌

$temp = curl_exec($ch);
curl_close($ch);
return $temp;

private$appid;

    private$appid;

}

private$appsecret;

    private$appsecret;

}
?>

publicfunction__construct()

  publicfunction__construct()

<script
src=”;
<?php
$jssdk = new JSSDK(“you APPID”, “you appSecret”);
$signPackage = $jssdk->getSignPackage();
?>
<script>
//
注意:全部的JS接口只好在民众号绑定的域名下调用,大伙儿号开拓者须要首先登场陆Wechat民众平台步入“公众号安装”的“成效设置”里填充“JS接口安全域名”。
// 若是发未来 Android
不能够分享自定义内容,请到官方网站下载最新的包覆盖安装,Android
自定义分享接口需提高至 6.0.2.58 版本及以上。
// 完整 JS-SDK
文书档案地址:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
//https://mp.weixin.qq.com/wiki?action=doc&id=mp1421141115&t=0.5189975742348003#dlwz1

{

  {

wx.config({
debug: true,//是不是调节和测量试验情势
appId: ‘应用软件id’,//切记改变APPID
timestamp: <?php echo
$signPackage[“timestamp”];?>,//必填,生成具名的年月戳
nonceStr: ‘<?php echo $signPackage[“nonceStr”];?>’,//
必填,生成具名的大肆串
signature: ‘<?php echo $signPackage[“signature”];?>’,//
必填,签名
jsApiList: [‘openLocation’] //
必填,供给运用的JS接口列表,八个里面用逗号隔断
});
wx.ready(function () {
wx.getLocation({
type: ‘wgs84′, //
默以为wgs84的gps坐标,假如要重临间接给openLocation用的水星坐标,可传唱’gcj02’
success: function (res) {

$this-appid = C(APPID卡塔尔国;//大伙儿号的appid

    $this->appid = C(‘应用程式ID’State of Qatar;//公众号的appid

    var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90

    var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。

    var speed = res.speed; // 速度,以米/每秒计

    var accuracy = res.accuracy; // 位置精度
    alert(latitude);
    alert(longitude);

  }

});

$this-appsecret = C(应用软件SECRET卡塔尔国;//群众号的秘钥

    $this->appsecret = C(‘应用程式SECRET’卡塔尔;//民众号的秘钥

});
</script>

}

  }

有白玉微瑕还望建议。

//调用js-sdk的签订左券包

  //调用js-sdk的具名包

publicfunctiongetSignPackage() {

  publicfunctiongetSignPackage() {

$jsapiTicket=$this-getJsApiTicket();

  $jsapiTicket=$this->getJsApiTicket();

// 注意 UMuranoL 应当要动态获取,不可能 hardcode.

  // 注意 U福睿斯L 应当要动态获取,不能 hardcode.(获取当前网页的url)

$protocol= (!empty($_SERVER[HTTPS]) $_SERVER[HTTPS]
!==off||$_SERVER[SERVER_PORT] == 443) ?;

  $protocol=  (澳门新葡亰娱乐官网,!empty($_SERVER[‘HTTPS’]) &&$_SERVER[‘HTTPS’]
 !==’off’||$_SERVER[‘SERVER_PORT’] == 443) ?””;

$url=$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI];

  $url=”$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]”;

//时间戳

  //时间戳

$timestamp= time();

  $timestamp= time();

//随机字符串获取

  //随机字符串获取

$nonceStr=$this-createNonceStr();

  $nonceStr=$this->createNonceStr();

// 这里参数的逐个要安分守纪 key 值 ASCII 码升序排序

  // 这里参数的逐个要根据 key 值 ASCII 码升序排序

$string=jsapi_ticket=$jsapiTicketnoncestr=$nonceStr×tamp=$timestampurl=$url;

  $string=”jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url”;

//生成字符串是用来署名用的

  //生成字符串是用来具名用的

$signature= sha1($string);

  $signature= sha1($string);

$signPackage=array(

  $signPackage=array(

appId=$this-appid,

   “appId”  =>$this->appid,

nonceStr=$nonceStr,

   “nonceStr”=>$nonceStr,

timestamp=$timestamp,

   “timestamp”=>$timestamp,

url=$url,

   “url”   =>$url,

signature=$signature,

   “signature”=>$signature,

rawString=$string

   “rawString”=>$string

);

  );

return$signPackage;

  return$signPackage;

}

 }

//使用会员卡领取的具名包

 //使用会员卡领取的签名包

publicfunctiongetHuiYuanSignPackage() {

 publicfunctiongetHuiYuanSignPackage() {

$apiTicket=$this-getApiTicket();

  $apiTicket=$this->getApiTicket();

// 注意 ULX570L 一定要动态获取,不可能 hardcode.

  // 注意 UPRADOL 应当要动态获取,不可能 hardcode.(获取当前网页的url)

$protocol= (!empty($_SERVER[HTTPS]) $_SERVER[HTTPS]
!==off||$_SERVER[SERVER_PORT] == 443) ?;

  $protocol=  (!empty($_SERVER[‘HTTPS’]) &&$_SERVER[‘HTTPS’]
 !==’off’||$_SERVER[‘SERVER_PORT’] == 443) ?””;

$url=$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI];

  $url=”$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]”;

//时间戳

  //时间戳

$timestamp= time();

  $timestamp= time();

//随机字符串获取

  //随机字符串获取

// $nonceStr = $this-createNonceStr();

  // $nonceStr = $this->createNonceStr();

// 这里参数的种种要根据 key 值 ASCII 码升序排序

  // 这里参数的次第要信守 key 值 ASCII 码升序排序

$string=$timestamp.$apiTicket.car_id;//card_id为投机成立的会员卡的id

  $string=$timestamp.$apiTicket.”car_id”;//card_id为温馨创办的会员卡的id

//生成字符串是用来具名用的

  //生成字符串是用来签字用的

$signature= sha1($string);

  $signature= sha1($string);

$signPackage=array(

  $signPackage=array(

timestamp=$timestamp,

   “timestamp”=>$timestamp,

signature=$signature,

   “signature”=>$signature,

);

  );

return$signPackage;

  return$signPackage;

}

 }

//获取会员卡的api_ticket

 //获取会员卡的api_ticket

publicfunctiongetApiTicket(){

 publicfunctiongetApiTicket(){

$data= json_decode(file_get_contents(api_ticket.json));

 $data= json_decode(file_get_contents(“api_ticket.json”));

if($data-expire_time time()) {

  if($data->expire_time < time()) {

$accessToken=$this-getAccessToken();

   $accessToken=$this->getAccessToken();

$url=_cardaccess_token=$accessToken;

   $url=””;

$res= json_decode($this-($url));

   $res= json_decode($this->httpGet($url));

$ticket=$res-ticket;

   $ticket=$res->ticket;

if($ticket) {

   if($ticket) {

$data-expire_time = time() + 7000;

    $data->expire_time = time() + 7000;

$data-jsapi_ticket =$ticket;

    $data->jsapi_ticket =$ticket;

$fp=fopen(api_ticket.json,w);

    $fp=fopen(“api_ticket.json”,”w”);

fwrite($fp, json_encode($data));

    fwrite($fp, json_encode($data));

fclose($fp);

    fclose($fp);

}

   }

}else{

  }else{

$ticket=$data-jsapi_ticket;

   $ticket=$data->jsapi_ticket;

}

  }

return$ticket;

  return$ticket;

}

 }

//获取随机字符串

 //获取随机字符串

privatefunctioncreateNonceStr($length= 16) {

 privatefunctioncreateNonceStr($length= 16) {

$chars=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;

  $chars=”abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789″;

$str=;

  $str=””;

for($i= 0;$i$length;$i++) {

  for($i= 0;$i<$length;$i++) {

$str.=substr($chars, mt_rand(0,strlen($chars) – 1), 1);

   $str.=substr($chars, mt_rand(0,strlen($chars) – 1), 1);

}

  }

return$str;

  return$str;

}

 }

//获取Access Token

  //获取Access Token

publicfunctiongetAccessToken(){

  publicfunctiongetAccessToken(){

//将json字符串转变为json对象

  //将json字符串调换为json对象(json_encode是将数组调换为json字符串,json_decode(“”,true卡塔尔国假诺加true是将json字符串转变为php数组,不加true转变为PHP对象)

$data= json_decode(file_get_contents(access_token.json));

  $data= json_decode(file_get_contents(“access_token.json”));

if($data-expire_time time()) {

  if($data->expire_time < time()) {

// 倘使是集团号用以下UHavalL获取access_token

   // 假若是集团号用以下UCR-VL获取access_token

$url=_type=client_credentialappid=$this-appidsecret=$this-appsecret;

   $url=””;

$res= json_decode($this-($url));

   $res= json_decode($this->httpGet($url));

$access_token=$res-access_token;

   $access_token=$res->access_token;

if($access_token) {

 if($access_token) {

$data-expire_time = time() + 7000;

    $data->expire_time = time() + 7000;

$data-access_token =$access_token;

    $data->access_token =$access_token;

$fp=fopen(access_token.json,w);

    $fp=fopen(“access_token.json”,”w”);

fwrite($fp, json_encode($data));

    fwrite($fp, json_encode($data));

fclose($fp);

    fclose($fp);

}

 }

}else{

  }else{

$access_token=$data-access_token;

   $access_token=$data->access_token;

}

  }

return$access_token;

  return$access_token;

}

  }

//获取jsapi_ticket(jsapi_ticket是大伙儿号用于调用WechatJS接口的临时票据卡塔尔

 //获取jsapi_ticket(jsapi_ticket是大众号用于调用WechatJS接口的偶然票据卡塔尔(قطر‎

privatefunctiongetJsApiTicket() {

  privatefunctiongetJsApiTicket() {

// jsapi_ticket 应该全局存款和储蓄与立异,以下代码以写入到文件中做示范

  // jsapi_ticket 应该全局存款和储蓄与校正,以下代码以写入到文件中做示范

$data= json_decode(file_get_contents(jsapi_ticket.json));

  $data= json_decode(file_get_contents(“jsapi_ticket.json”));

if($data-expire_time time()) {

  if($data->expire_time < time()) {

$accessToken=$this-getAccessToken();

   $accessToken=$this->getAccessToken();

// 假如是厂商号用以下 ULX570L 获取 ticket

   // 假如是商店号用以下 U福睿斯L 获取 ticket

// $url = _jsapi_ticket?access_token=$accessToken;

   // $url =
“”;

$url=_token=$accessToken;

   $url=””;

$res= json_decode($this-($url));

   $res= json_decode($this->httpGet($url));

$ticket=$res-ticket;

   $ticket=$res->ticket;

if($ticket) {

   if($ticket) {

$data-expire_time = time() + 7000;

    $data->expire_time = time() + 7000;

$data-jsapi_ticket =$ticket;

    $data->jsapi_ticket =$ticket;

$fp=fopen(jsapi_ticket.json,w);

    $fp=fopen(“jsapi_ticket.json”,”w”);

fwrite($fp, json_encode($data));

    fwrite($fp, json_encode($data));

fclose($fp);

    fclose($fp);

}

   }

}else{

  }else{

$ticket=$data-jsapi_ticket;

   $ticket=$data->jsapi_ticket;

}

  }

return$ticket;

  return$ticket;

}

 }

//获取顾客的openid

  //获取顾客的openid

publicfunctionopenId(){

  publicfunctionopenId(){

$url=$_SERVER[HTTP_HOST] .$_SERVER[REQUEST_URI];

  $url=$_SERVER[‘HTTP_HOST’] .$_SERVER[‘REQUEST_URI’];

if(!isset($_GET[code])) {

    if(!isset($_GET[‘code’])) {

//获取组装的url

     //获取组装的url

$openidUrl=$this-snsapi_base($url);

      $openidUrl=$this->snsapi_base($url);

redirect($openidUrl);

      redirect($openidUrl);

}else{

    }else{

$openidAccess_token=$this-openidAccess_token($_GET[code]);

      $openidAccess_token=$this->openidAccess_token($_GET[‘code’]);

return$openidAccess_token;

      return$openidAccess_token;

}

    }

}

  }

//获取微信顾客的opnid

   //获取Wechat客户的opnid

publicfunctiongetOpenId($openid,$access_token)

  publicfunctiongetOpenId($openid,$access_token)

{

  {

$userInfo=$this-getUserInfo($openid,$access_token);

    $userInfo=$this->getUserInfo($openid,$access_token);

return$userInfo;

    return$userInfo;

}

  }

publicfunctionsnsapi_base($redirect_uri,$scope=snsapi_userinfo,$state=
0)

   publicfunctionsnsapi_base($redirect_uri,$scope=”snsapi_userinfo”,$state=
0)

{

  {

$appId=$this-appid;

    $appId=$this->appid;

$url=;

    $url=””;

$url.=?appid=$appId;

    $url.=”?appid=$appId”;

$url.=redirect_uri=$redirect_uri;

    $url.=”&redirect_uri=”;

$url.=response_type=code;

    $url.=”&response_type=code”;

$url.=scope=$scope;

    $url.=”&scope=$scope”;

$url.=state=$state#wechat_redirect;

    $url.=”&state=$state#wechat_redirect”;

return$url;

    return$url;

}

  }

publicfunctionopenidAccess_token($code){

publicfunctionopenidAccess_token($code){

$appId=$this-appid;

    $appId=$this->appid;

$appSecret=$this-appsecret;

    $appSecret=$this->appsecret;

$url=_token?appid=$appIdsecret=$appSecretcode=$codegrant_type=authorization_code;

    $url=””;

returnjson_decode($this-($url),true);

    returnjson_decode($this->httpGet($url),true);

}

  }

//获取客商新闻

  //获取顾客新闻

publicfunctiongetUserInfo($openid,$access_token){

  publicfunctiongetUserInfo($openid,$access_token){

$url=_token=$access_tokenopenid=$openidlang=zh_CN ;

  $url=”
“;

returnjson_decode($this-($url),true);

    returnjson_decode($this->httpGet($url),true);

//请求

   //请求

}

  }

privatefunction($url) {

privatefunctionhttpGet($url) {

$curl= curl_init();

  $curl= curl_init();

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl, CURLOPT_TIMEOUT, 500);

  curl_setopt($curl, CURLOPT_TIMEOUT, 500);

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

curl_setopt($curl, CURLOPT_URL,$url);

  curl_setopt($curl, CURLOPT_URL,$url);

$res= curl_exec($curl);

  $res= curl_exec($curl);

curl_close($curl);

  curl_close($curl);

return$res;

  return$res;

}

 }

}

}

 

You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图