菜单

WeChat package calls WeChat signature package class library

2017年11月22日 - PHPer

Nonsense, say, directly to everyone posted code, the specific code as follows:

 

Code is as follows

<? PHP

NamespaceHome\Model;

UseThink\Model;

ClassWechatModelextendsModel {

Private$_token=”// token

Private$appid;

Private$appsecret;

(publicfunction__construct)

{

$this-> appid = C (‘APPID’); / / the number of public appid

$this-> appsecret = C (‘APPSECRET’); / / the number of public key

}

/ / js-sdk call signature package

(publicfunctiongetSignPackage) {

($jsapiTicket=$this-> getJsApiTicket);

/ / / / URL must be acquired dynamically (not hardcode. access to the current web page URL)

$protocol= (! Empty ($_SERVER[‘HTTPS’]) & & $_SERVER[‘HTTPS’]! ==’off’||$_SERVER[‘SERVER_PORT’] = = 443) “https://”: “http://?”;

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

/ / time stamp

($timestamp= time);

/ random string access

($nonceStr=$this-> createNonceStr);

/ / here the order of the parameters according to the key value of ASCII code in ascending order

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

/ / string is used to use signature generation

$signature= SHA1 ($string);

($signPackage=array

“AppId” => $this-> appid,

“NonceStr” => $nonceStr,

“Timestamp” => $timestamp,

“URL” => $url,

“Signature” => $signature,

“RawString” => $string

);

Return$signPackage;

}

/ / use membership card to receive the package signature

(publicfunctiongetHuiYuanSignPackage) {

($apiTicket=$this-> getApiTicket);

/ / / / URL must be acquired dynamically (not hardcode. access to the current web page URL)

$protocol= (! Empty ($_SERVER[‘HTTPS’]) & & $_SERVER[‘HTTPS’]! ==’off’||$_SERVER[‘SERVER_PORT’] = = 443) “https://”: “http://?”;

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

/ / time stamp

($timestamp= time);

/ random string access

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

/ / here the order of the parameters according to the key value of ASCII code in ascending order

$string=$timestamp.$apiTicket. “car_id”; //card_id is to create their own membership card ID

/ / string is used to use signature generation

$signature= SHA1 ($string);

($signPackage=array

“Timestamp” => $timestamp,

“Signature” => $signature,

);

Return$signPackage;

}

/ / obtain the membership card of api_ticket

(publicfunctiongetApiTicket) {

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

}

/ / get random

{privatefunctioncreateNonceStr ($length= 16)

$chars= “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789”;

$str=””;

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

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

}

Return$str;

}

/ / get Access Token

(publicfunctiongetAccessToken) {

/ JSON string conversion to JSON (json_encode is an array is converted to a JSON string, json_decode (“true”) if true is a JSON string into the PHP array, and true conversion to PHP)

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

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

/ / if the enterprise is obtained with the following 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;

}

/ / get jsapi_ticket (jsapi_ticket is a public number for temporary bill calling WeChat JS interface)

(privatefunctiongetJsApiTicket) {

/ / jsapi_ticket should be global storage and update, the following code to write to the file in the sample

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

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

($accessToken=$this-> getAccessToken);

/ / if the enterprise is obtained with the following 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;

}

/ / get the user’s openid

(publicfunctionopenId) {

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

If (! Isset ($_GET[‘code’])) {

/ / get assembled URL

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

Redirect ($openidUrl);

}else{

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

Return$openidAccess_token;

}

}

/ access to WeChat users opnid

PublicfunctiongetOpenId ($openid, $access_token)

{

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

Return$userInfo;

}

($redirect_uri publicfunctionsnsapi_base, $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;

}

PublicfunctionopenidAccess_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”;

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

}

/ / get the user information

PublicfunctiongetUserInfo ($openid, $access_token) {

$url= “https://api.weixin.qq.com/sns/userinfo? Access_token=$access_token& openid=$openid& lang=zh_CN”;

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

/ / request

}

PrivatefunctionhttpGet ($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;

}

}

 

发表评论

电子邮件地址不会被公开。