Log in to your WeChat platform and click "Official Account Settings".
2
Click "Function Settings" and then click "Settings".
3
Set the security domain name of the JS interface. The first-level domain name filled in here is without www and /cgi-bin/token?grant_type=client_credential&appid=' .'Your AppID'.'&secret=' .'Your AppSecret');
$res = json_decode($res, true);
$token = $res['access_token'];
// Note: The obtained token needs to be cached here ( Or write to the database)
///cgi-bin/token cannot be accessed frequently, and there is a daily limit
//The validity period of the token returned through this interface is currently 2 hours. After the token expires, the JS-SDK cannot be used.
// Therefore, the token value is cached here for 1 hour, which is less than 2 hours. After the cache expires, obtain a new token from the interface, so
// that the token expiration can be avoided.
// S() is the cache function of ThinkPhp. If you are not using the ThinkPhp framework, you can use your cache function or use the database to save it.
S('access_token', $token, 3600);
}
return $token;
}
Note: The length of the returned access_token must be at least 512 bytes. Interface return value:
{"access_token":"ACCESS_TOKEN","expires_in":7200}
{"access_token":"vdlThyTfyB0N5eMoi3n_aMFMKPuwkE0MgyGf_0h0fpzL8p_hsdUX8VGxz5oSXuq5dM69lxP9w BwN9Yzg-0kVHY33BykRC0YXZZZ-WdxEic4","expires_in": 7200}
6
Get the jsapi ticket. jsapi_ticket is a temporary ticket used by public accounts to call the WeChat JS interface. Under normal circumstances, the validity period of jsapi_ticket is 7200 seconds and 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("get access token error.");
break;
}
$url2 = sprintf("/cgi-bin/ticket/getticket?access_token=%s&type=jsapi",
$token);
$res = file_get_contents($url2);
$res = json_decode($res, true);
$ticket = $res['ticket'];
p>// Note: The obtained ticket needs to be cached here (or written to the database)
// Ticket, like token, cannot be obtained by frequently accessing the interface. Each time After getting it, we save it.
S('wx_ticket', $ticket, 3600);
}while(0);
return $ticket;
}
Interface return value:
{"errcode":0,"errmsg":"ok","ticket":"sM4AOVdWfPE4DxkXGEs8VMKv7FMCPm-I98-klC6SO3Q3AwzxqljYWtzTCxIH9hDOXZCo9cgfHI6kwbe_YWtOQg","expire s_in": 7200}
7
Signature, connect jsapi_ticket, noncestr, timestamp, and shared url in alphabetical order to perform sha1 signature.
noncestr is any string you set.
timestamp is the timestamp.
$timestamp = time();
$wxnonceStr = "Any string";
$wxticket = wx_get_jsapi_ticket();
$wxOri = sprintf("jsapi_ticket=%s&noncestr=%s×tamp=%s&url=%s",
$wxticket, $wxnonceStr, $timestamp,
'To share url(from/open/js/jweixin-1.0.0.js">
// WeChat configuration
wx.config({
debug: false,
appId: "Your AppID",
timestamp: 'Previous step Generated timestamp',
nonceStr: 'String in the previous step',
signature: 'Signature generated in the previous step',
jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] // Function list, what functions of JS-SDK we want to use
});
//The ready method will be executed after the config information is verified , all interface calls must be made after the config interface obtains the result. config is a client-side asynchronous operation, so if the relevant interface needs to be called when the page is loaded, the relevant interface must be called in the ready function to ensure correct execution. The interface that is called only when triggered by the user can be called directly and does not need to be placed in the ready function.
wx.ready(function(){
// Get the click status of the "Share to Moments" button and customize the sharing content interface
wx.onMenuShareTimeline ({
title: 'Share title', // Share title
link: "Shared url, starting with http or https",
imgUrl: "The url of the share icon, starting with http or https" // Share icon
});
// Get the click status of the "Share with friends" button and the custom sharing content interface
wx.onMenuShareAppMessage({
title: 'Share title', // Share title
desc: "Share description", // Share description p>
link: "The url to share starts with http or https",
imgUrl: "The url to share the icon starts with http or https", // Share icon
< p> type: 'link', // Sharing type, music, video or link, default is link if left blank});
});