2
Click "Function Settings", and then click "Settings".
three
Set the JS interface security domain name. This is a first-level domain name, without www and /cgi-bin/token? Authorization type = customer certificate. Appid= "."Your appid "."&; Secret='.' Your app secret');
$res = json_decode($res,true);
$ token = $ RES[' access _ token '];
//Note: You need to cache the acquired token (or write it into the database) here.
//You can't visit /cgi-bin/token frequently, and the number of times per day is limited.
//Tokens returned through this interface are currently valid for 2 hours. After the token expires, JS-SDK cannot be used.
//So the cached token value here is 1 hour, which is less than 2 hours. When the cache fails, a new token is obtained from the interface, so
//Token invalidation can be avoided.
// S () is a cache function of ThinkPhp. If you use a non-ThinkPhp framework, you can use your caching function or use a database to save it.
S('access_token ',$token,3600);
}
Return $ token
}
Note: The length of the returned access_token should be at least 5 12 bytes. Interface return value:
{"access_token":"ACCESS_TOKEN "," expires_in":7200}
{ " access _ token ":" vdlthytfyb 0n 5 emo i3 n _ amfmkpuwke 0 mgygf _ 0h 0 fpzl 8 p _ HSD UX 8 VG xz 5 OS xuq 5dm 69 LX p9 wbwn 9 yzg-0k vhy 33 bykrc 0 yxzzz-wdx EIC 4 "," expires_in":7200}
six
Get tickets for jsapi. Jsapi_ticket is a temporary ticket used by WeChat official account to call WeChat JS interface. Generally, the validity period of jsapi_ticket is 7200 seconds, which is obtained through access_token.
Function wx_get_jsapi_ticket(){
$ ticket =
Do {
$ ticket = S(' wx _ ticket ');
If (! Empty ($ ticket) (
Break;
}
$ token = S(' access _ token ');
if (empty($token)){
wx _ get _ token();
}
$ token = S(' access _ token ');
if (empty($token)) {
LogErr ("Error getting access token." );
Break;
}
$ URL 2 = sprintf("/CGI-bin/ticket/getticket? Access token =% s & amptype=jsapi ",
$ token);
$ RES = file _ get _ contents($ URL 2);
$res = json_decode($res,true);
$ ticket = $ RES[' ticket '];
//Note: You need to cache the obtained tickets here (or write them into the database).
//Like token, ticket can't access the interface frequently to get it. We will keep it after every acquisition.
S('wx_ticket ',$ticket,3600);
} while(0);
Return $ tickets;
}
Interface return value:
{"errcode":0," errmsg":"ok "," ticket ":" sm 4 aovdwfpe 4 dxk xges 8 vmkv 7 fmcpm-I98-klc 6 so 3 awzxql jywtztcxih 9 hdoxzco 9 CGF hi 6 kwbe _ YWtOQg "," expires_in":7200}
seven
Signature: connect jsapi_ticket, noncestr, timestamp and shared url in alphabetical order, and sign sha 1
Noncestr is any string you set.
The timestamp is a timestamp.
$ timestamp = time();
$ wxconcestr = "any string";
$ wx ticket = wx _ get _ jsapi _ ticket();
$ wxOri = sprintf(" jsapi _ ticket = % s & amp; noncestr = % s× tamp = % s & ampurl=%s ",
$wxticket,$ wxnonceStr,$timestamp,
Url to share (from/open/js/jweixin-1.0.0.js "> <; /script & gt;
& ltscript type = " text/JavaScript " & gt;
//WeChat configuration
wx.config({
Debugging: false,
AppID: "your AppId",
Timestamp: "Timestamp generated in the previous step",
NonceStr: "the string in the previous step",
Signature: "Signature generated in the previous step",
JSAPI list: ['OnMenuSharePointTimeline',' OnMenuShareAppMessage ']// Function list, which function of JS-SDK should we use?
});
The ready method will be executed after verifying the configuration information. All interface calls must be made after the config interface obtains the results. Config is an asynchronous operation of the client. Therefore, if you need to call the relevant interface when the page is loaded, you must call it in the ready function to ensure correct execution. For interfaces that are called only when triggered by the user, they can be called directly without being put into the ready function.
wx.ready(function(){
//Get the click status of the "Share to Friends Circle" button, and customize the interface for sharing content.
wx.onMenuShareTimeline({
Title:' Shared Title',//Shared Title
Link: "shared url, starting with http or https",
ImgUrl: "Url of shared icon, starting with http or https"//shared icon
});
//Get the click status of the "Share with Friends" button, and customize the interface for sharing content.
wx.onMenuShareAppMessage({
Title:' Shared Title',//Shared Title
Desc: "shared description",//shared description
Link: "shared url, starting with http or https",
ImgUrl: "Url of shared icon, starting with http or https",//shared icon
Type:' Link',//Sharing Type, Music, Video or Link; if left blank, the default value is Link.
});
});
& lt/script & gt;