Current location - Quotes Website - Signature design - What is the use of WeChat’s open js sdk?
What is the use of WeChat’s open js sdk?

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">