Provides various official and user-released code examples and code reference. You are welcome to exchange and learn about the new version of jssdk provided yesterday. Here we modified it and supported the 3.2 version of the command space, debugging mode added at the same time!
/**
* Official documentation: http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
* Example: http://demo.open.weixin.qq.com/jssdk/sample.zip
*
* The JSSDK class mainly modifies the mechanism for saving session information. In this example, files are used. The ThinkPHP cache mechanism is used here. For details, refer to the official example documentation.
* The debugging mode is added. The call example is as follows:
* $ Jssdk = new JSSDK (C ('wx _ APPID '), C ('wx _ SECRET '));
* $ Jssdk-> debug = true; // enable the local debugging mode. Put the two official json files in the same directory as the index. php file!
* $ SignPackage = $ jssdk-> GetSignPackage ();
* @ Namespace version
* @ Author Gump (QQ: 33808 624)
* @ Date
*/
Namespace Com;
Class JsSdk {
Private $ appId;
Private $ appSecret;
Public $ debug = false;
Public function _ construct ($ appId, $ appSecret ){
$ This-> appId = $ appId;
$ This-> appSecret = $ appSecret;
}
Public function getSignPackage (){
$ JsapiTicket = $ this-> getJsApiTicket ();
$ Url = "http: // $ _ SERVER [HTTP_HOST] $ _ SERVER [REQUEST_URI]";
$ Timestamp = time ();
$ NonceStr = $ this-> createNonceStr ();
// Here, the parameter order is sorted in ascending order of the key value ASCII code
$ 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;
}
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;
}
Private function getJsApiTicket (){
// Debug mode
If ($ this-> debug ){
// Jsapi_ticket should be stored and updated globally. The following code is used as an example to write data to a file.
$ Data = json_decode (file_get_contents ("jsapi_ticket.json "));
} Else {
// Read from the cache, based on the ThinkPHP cache Mechanism
$ Data = (object) (S ('jsapi _ ticket_json '));
}
If ($ data-> expire_time <time ()){
$ AccessToken = $ this-> getAccessToken ();
$ Url = "http://api.weixin.qq.com/cgi-bin/ticket/getticket? Type = 1 & access_token = $ accessToken ";
$ Res = json_decode ($ this-> httpGet ($ url ));
$ Ticket = $ res-> ticket;
If ($ ticket ){
$ Data-> expire_time = time () + 7200;
$ Data-> jsapi_ticket = $ ticket;
// Debug mode
If ($ this-> debug ){
$ Fp = fopen ("jsapi_ticket.json", "w ");
Fwrite ($ fp, json_encode ($ data ));
Fclose ($ fp );
} Else {
// Cache the object as an array
S ('jsapi _ ticket_json ', (array) $ data );
}
}
} Else {
$ Ticket = $ data-> jsapi_ticket;
}
Return $ ticket;
}
Private function getAccessToken (){
// Debug mode
If ($ this-> debug ){
// Access_token should be stored and updated globally. The following code is used as an example to write data to a file.
$ Data = json_decode (file_get_contents ("access_token.json "));
Dump ($ data );
Die ();
} Else {
// Read the array from the cache and convert it into an object
$ Data = (Object) (S ('Access _ token. json '));
}
If ($ data-> expire_time <time ()){
$ 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;
// Debug mode
If ($ this-> debug ){
$ Fp = fopen ("access_token.json", "w ");
Fwrite ($ fp, json_encode ($ data ));
Fclose ($ fp );
} Else {
// Cache Array
S ('Access _ token. json', (array) $ data );
}
}
} Else {
$ Access_token = $ data-> access_token;
}
Return $ access_token;
}
Private function httpGet ($ url ){
$ Curl = curl_init ();
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, true );
Curl_setopt ($ curl, CURLOPT_TIMEOUT, 500 );
Curl_setopt ($ curl, CURLOPT_URL, $ url );
$ Res = curl_exec ($ curl );
// Error Detection
$ Error = curl_error ($ curl );
Curl_close ($ curl );
// Throw an exception when an error occurs.
If ($ error) throw new \ Exception ('request error (Table check whether access is performed under the authorized Domain Name): '. $ error );
Return $ res;
}
}
Com.rar (2.28 KB download: 202 times)
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB