PHP WeChat shared development details, php shared details

Source: Internet
Author: User

PHP shared development details, php shared details

Share the development experience of a php client. Sometimes, when a project or project needs to be shared and then perform a series of events, we need to get the share action, that is to say, we already know that this item has been shared, so the default sharing is obviously not acceptable. We need to configure sharing by ourselves, when the user shares the information, we use the predefined program, so that we can easily implement what we have done after sharing.

The main JavaScript code on the page is as follows:

<Script type = "text/javascript" src = "http://res.wx.qq.com/open/js/jweixin-1.0.0.js"> </script> <script> wx. config ({debug: false, // enable the debugging mode. The returned values of all called APIs are displayed in the client alert. To view the input parameters, you can open them on the pc, the parameter information is output through log and printed only on the pc end. AppId: '{$ appid}', // required. The only identifier of the public account is timestamp: {$ timestamp}. // required. The signature timestamp is nonceStr: '{$ nonceStr}', // required. The random signature string signature: '{$ signature}' is generated. // required. For the signature, see Appendix 1 jsApiList: ['onmenushareappmessage', 'onmenusharetimeline'] // required. List of JS interfaces to be used. For a list of all JS interfaces, see Appendix 2}); wx. ready (function () {// share with your friend wx. onMenuShareAppMessage ({title: {$ title}, // share the title here $ title can be transferred on the Controller side or on the page transmission page for explanation below: desc: {$ desc }, // share description link: {$ link}, // share link imgUrl: {$ imgurl}, // share icon type: '', // share type, music, video, or link. The default value is link dataUrl: ''. // if the type is music or video, the data link is provided. The default value is success: function () {alert ('shared successfully') ;}, cancel: function () {// The callback function executed after the user cancels the sharing // alert ('cancel sharing ') ;}}); // share it with the wx circle of friends. onMenuShareTimeline ({title: {$ title}, // share title desc: {$ desc}, // share description link: {$ link}, // share link imgUrl: {$ imgurl}, // share icon success: function () {// callback function executed after the user confirms the sharing}, cancel: function () {// callback function executed after the user cancels the sharing}) ;}); </script>
 

When wx. after config is configured, the program will continue to share with you, for debugging, you can change false to true. If it is configured, OK is displayed normally. If it is not configured, changing it to true will not bring up any pop-up effect.

Wx. config requires the Controller to transmit four parameters: appId, timestamp, nonceStr, and signature. The controller code is as follows:

<? Php $ jssdk = new \ Home \ Util \ JSSDK (C ('appid '), C ('secret ')); // here, the items in C are the appid and secret of the public account you are using. The two items can be obtained on the public platform without detailed explanations and no additional comments can be found (explanations) the JSSDK File Code is under $ signPackage =$ jssdk-> GetSignPackage (); $ this-> assign ('appid ', $ signPackage ["appid"]); $ this-> assign ('timestamp', $ signPackage ["timestamp"]); $ this-> assign ('noncestr ', $ signPackage ["nonceStr"]); $ this-> assign ('signature', $ signPackage ["signature"]);

The wx. config has been configured here, and the title and other information can be used as an example here.

$ This-> assign ('title', $ title );

JSSDK file code:

<? Phpnamespace Home \ Util; use Think \ Controller; class JSSDK {private $ appId; private $ appSecret; public function _ construct ($ appId, $ appSecret) {$ this-> appId = $ appId; $ this-> appSecret = $ appSecret;} public function getSignPackage () {$ jsapiTicket = $ this-> getJsApiTicket (); // note that the URL must be dynamically obtained and cannot be hardcode. $ protocol = (! Empty ($ _ SERVER ['https']) & $ _ SERVER ['https']! = 'Off' | $ _ SERVER ['server _ port'] = 443 )? "Https: //": "http: //"; $ url = "$ protocol $ _ SERVER [HTTP_HOST] $ _ SERVER [REQUEST_URI]"; $ timestamp = time (); $ nonceStr = $ this-> createNonceStr (); // The Order of parameters here should be sorted by the key value ASCII code in ascending order $ string = "jsapi_ticket = $ jsapiTicket & noncestr = $ nonceStr & timestamp = $ timestamp & url = $ url "; $ signature = sha1 ($ string); $ signPackage = array ("appId" => $ this-> appId, "nonceStr" => $ nonceStr, "timestamp" => $ timestamp, "url" => $ u Rl, "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 ;} public function getJsApiTicket () {// 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 ($ this-> get_php_file ("jsapi_ticket.php"); if ($ data-> expire_time <time ()) {$ accessToken = $ this-> getAccessToken (); // get ticket with the following URL for the enterprise ID // $ url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket? Access_token = $ accessToken "; $ url =" https://api.weixin.qq.com/cgi-bin/ticket/getticket? Access_token = $ accessToken & type = jsapi "; $ res = json_decode ($ this-> httpGet ($ url); $ ticket = $ res-> ticket; // var_dump ($ url); if ($ ticket) {$ data-> expire_time = time () + 7000; $ data-> jsapi_ticket = $ ticket; $ this-> set_php_file ("jsapi_ticket.php", json_encode ($ data) ;}} else {$ ticket = $ data-> jsapi_ticket;} return $ ticket ;} public function getAccessToken () {// access_token should be stored and updated globally. The following code is written to the file. Example $ data = json_decode ($ this-> get_php_file ("access_token.php"); if ($ data-> expire_time <time ()) {// get access_token with the following URL for the enterprise ID // $ url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken? Corpid = $ this-> appId & Cortana cret = $ this-> appSecret "; $ url =" https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = $ this-> appId & secret = $ this-> appSecret "; $ res = $ this-> getJson ($ url ); $ access_token = $ res ['Access _ token']; // var_dump ($ res); if ($ access_token) {$ data-> expire_time = time () + 7000; $ data-> access_token = $ access_token; $ this-> set_php_file ("access_token.php", json_encode ($ data); }} else {$ access_token = $ data-> access_token ;} return $ access_token; // $ aa = $ acce Ss_token; // var_dump ($ aa) ;}// get access_token public function getJson ($ url) {$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); $ output = curl_exec ($ ch); curl_close ($ ch); // var_dump (json_decode ($ output, true); return json_decode ($ output, true );}// Get ticket private function httpGet ($ url) {$ curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL, $ url); curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, true ); // curl_setopt ($ curl, CURLOPT_TIMEOUT, 500); // to ensure the security of data transmission between a third-party server and the server, all interfaces are called using https, you must use the following two lines of code to enable ssl security verification. // If the Code fails to be verified here during deployment, download the new certificate identification file from the http://curl.haxx.se/ca/cacert.pem. Curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, FALSE); $ res = curl_exec ($ curl); // var_dump ($ res ); curl_close ($ curl); return $ res;} private function get_php_file ($ filename) {return trim (substr (file_get_contents ($ filename), 15 )); // echo trim (substr (file_get_contents ($ filename), 15); die; // $ aa = trim (substr (file_get_contents ($ filename), 15);} pri Vate function set_php_file ($ filename, $ content) {$ fp = fopen ($ filename, "w"); fwrite ($ fp, "<? Php exit ();?> ". $ Content); fclose ($ fp );}}

Here, the sharing will come to an end, but there are flaws. What if we want to dynamically PASS Parameters? That is to say, when the page information is wx. after config is configured, we suddenly want to assign a new value to $ title and other variable information. What should I do?

We can share the following information in a method (rough code ):

<Script> var zl = function (title, link, imgurl, desc) {wx. ready (function () {// share with your friend wx. onMenuShareAppMessage ({title: title, // share title desc: desc, // share description link: link, // share link imgUrl: imgurl, // share icon type :'', // share type: music, video, or link. If this parameter is left blank, the default value is link dataUrl: ''. // if the type is music or video, the data link is provided. The default value is success: function () {alert ('shared successfully');}, cancel: function () {// The callback function executed after the user cancels the sharing // alert ('cancel sharing ') ;}}); // share it with the wx circle of friends. onMenuShareTimeline ({title: title, // share title desc: desc, // share description link: link, // share link imgUrl: imgurl, // share icon success: function () {// callback function executed after the user confirms the sharing}, cancel: function () {// The callback function executed after the user cancels the sharing. // alert ('share canceled ') ;}}) ;}; </script>

Explanation: I have already configured the title and other information in the share on the Controller's page. So after the configuration, I want to re-provide the title value on the page. This is the method. The page copy code is as follows:

<script>zl(title,link,imgurl,desc);</script>

For more information about this function and other functions, see the manual.

Link: https://mp.weixin.qq.com/wiki/14/9f9c82c1af308e3b14ba9b973f99a8ba.html (share function in the manual web development --> JS-SDK on the right side of the page can be seen)

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.