Php WeChat custom sharing code-PHP source code

Source: Internet
Author: User
On many large websites, we can see that we can share data to QQ or other platforms. Next we will look at a php version of custom sharing code, there is no problem with the code according to the official development. On many large websites, we can see that we can share data to QQ or other platforms. Next we will look at a php version of custom sharing code, there is no problem with the code according to the official development.

Script ec (2); script


Share the subscription number or service number to be authenticated.

Php code (thinkphp ):

$ Appid = 'xxx ';
$ Appsecret = 'xxxx ';

$ Timestamp = time ();
$ Noncestr = $ this-> getRandStr (15 );

// Dump ();

$ Url = 'https: // api.weixin.qq.com/cgi-bin/ticket/getticket? Access_token = '. $ this-> get_token ($ appid, $ appsecret).' & type = jsapi ';
$ Ret_json = $ this-> curl_get_contents ($ url );
$ Ret = json_decode ($ ret_json );
$ Ticket = $ ret-> ticket;
// Var_dump ($ ret );
$ Strvalue = 'jsapi _ ticket = '. $ ticket. '& noncestr = '. $ noncestr. '& timestamp = '. $ timestamp. '& url = http ://'. $ _ SERVER ['HTTP _ host']. $ _ SERVER ['request _ URI '];
$ Signature = sha1 ($ strvalue );

$ This-> assign ('timestamp', $ timestamp );
$ This-> assign ('noncestr', $ nonceStr );
$ This-> assign ('signature', $ signature );


Function get_token ($ appid, $ appsecret ){
If (S ('Access _ token') return S ('Access _ token ');
$ Url = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = $ appid & secret = $ appsecret ";
$ Ret_json = $ this-> curl_get_contents ($ url );
$ Ret = json_decode ($ ret_json );
If ($ ret-> access_token ){
S ('Access _ token', $ ret-> access_token, 7200 );
Return $ ret-> access_token;
}
}


Function is_weixin (){
If (strpos ($ _ SERVER ['HTTP _ USER_AGENT '], 'micromessenger ')! = False ){
Return true;
}
Return false;
}

Function getRandStr ($ length ){
$ Str = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz ';
$ RandString = '';
$ Len = strlen ($ str)-1;
For ($ I = 0; $ I <$ length; $ I ++ ){
$ Num = mt_rand (0, $ len );
$ RandString. = $ str [$ num];
}
Return $ randString;
}

 

Function curl_get_contents ($ url ){
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_TIMEOUT, 1 );
Curl_setopt ($ ch, CURLOPT_MAXREDIRS, 200 );
Curl_setopt ($ ch, CURLOPT_USERAGENT, _ USERAGENT _);
Curl_setopt ($ ch, CURLOPT_REFERER, _ REFERER _);
@ Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false );
$ R = curl_exec ($ ch );
Curl_close ($ ch );
Return $ r;
}

Js Code:

Need to introduce: http://res.wx.qq.com/open/js/jweixin-1.0.0.js

Wx. config ({
Debug: false, // enable the debugging mode. The returned values of all called APIs are displayed on the alert client. To view the input parameters, open them on the pc, the parameter information is output through log and printed only on the pc end.
AppId: 'wxae7c36a1349c5868 ', // required. It is a unique public ID.
Timestamp: '{$ timestamp}', // required. The signature timestamp is generated.
NonceStr: '{$ nonceStr}', // required. A Random signature string is generated.
Signature: '{$ signature}', // required. For signature, see Appendix 1.
JsApiList: ['onmenusharetimeline', 'onmenushareappmessage'] // required. List of JS interfaces to be used. For the list of all JS interfaces, see appendix 2.
});

Wx. ready (function (){
Wx. onMenuShareTimeline ({
Title: '{$ contentInfo. title}', // share the title
Link: window. location. href, // share link
ImgUrl: 'http: // '+ window. location. host +' {$ categoryInfo. image} ', // share icon
Success: function (){
// The callback function executed after the user confirms the sharing
// Alert (1111 );
// Fxfunc ();
},
Cancel: function (){
// The callback function executed after the user cancels the sharing
// Alert ("You have canceled sharing ");
}
});

 

Wx. onMenuShareAppMessage ({
Title: '{$ contentInfo. title}', // share the title
Desc: removeHTMLTag ('{$ contentInfo. content}'), // share the description
Link: window. location. href, // share link
ImgUrl: 'http: // '+ window. location. host +' {$ categoryInfo. image} ', // share icon
Type: '', // share type, such as music, video, or link. If this parameter is not specified, the default value is link.
DataUrl: '', // if the type is music or video, provide a data link. The default value is null.
Success: function (){
// The callback function executed after the user confirms the sharing
// Fxfunc ();
},
Cancel: function (){
// Alert ("You have canceled sharing ");
// The callback function executed after the user cancels the sharing
}
});
// After the config information is verified, the ready method is executed. All interfaces must be called after the config interface obtains the result. config is a client asynchronous operation, therefore, if you need to call the interface when loading the page, you must place the interface in the ready function to ensure proper execution. You can directly call the interface that is called only when the user triggers the operation, and do not need to put it in the ready function.
});

Function removeHTMLTag (str ){
Str = str. replace (/<\/? [^>] *>/G, ''); // remove HTML tags
Str = str. replace (/[|] * \ n/g, '\ n'); // remove the trailing blank line
// Str = str. replace (/\ n [\ s |] * \ r/g, '\ n'); // Remove extra empty rows
Str = str. replace (// ig, ''); // remove
Return str;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.