This article mainly introduces the custom sharing function based on thinkPHP, and analyzes the related operation skills of thinkPHP calling the interface to implement the custom sharing function based on the instance form, you can refer to the examples in this article to describe the custom sharing function based on thinkPHP. We will share this with you for your reference. The details are as follows:
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.
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.'×tamp='.$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 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: 'wxae7c36a1349c5868 ', // required. the unique timestamp:' {$ timestamp} 'of the public account is required. the signature timestamp is nonceStr: '{$ nonceStr}', // required. the random signature string signature: '{$ signature}' is generated. // required. for the signature, see Appendix 1 jsApiList: ['onmenusharetimeline', 'onmenushareappmessage'] // required. list of JS interfaces to be used. For a 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: // '+ wind Ow. location. host + '{$ categoryInfo. image} ', // share icon success: function () {// callback function executed after the user confirms the sharing; // alert (1111); // fxfunc ();}, cancel: function () {// callback function executed after the user cancels the sharing // alert ("You canceled sharing") ;}}); wx. onMenuShareAppMessage ({title: '{$ contentInfo. title} ', // share the title desc: removeHTMLTag (' {$ contentInfo. content} '), // share description link: window. location. href, // share link imgUrl: 'http: // '+ window. location. host + '{$ categoryInfo. imag E} ', // share icon type: '', // share type, music, video, or link. if not specified, the default value is link dataUrl :'', // if the type is music or video, a data link is provided. the default value is success: function () {// The callback function executed after the user confirms the sharing // fxfunc () ;}, cancel: function () {// alert ("You canceled sharing "); // The callback function executed after the user cancels the sharing}); // The ready method is executed after the config information is verified. all interfaces must be called after the result is obtained by the config interface, config is a client-side 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 correct execution. For interfaces that are called only when triggered by the user, you can directly call them without putting them in the ready function .}); Function removeHTMLTag (str) {str = str. replace (/<\/? [^>] *>/G, ''); // remove HTML tag str = str. replace (/[|] * \ n/g, '\ n'); // removes the trailing blank line. // str = str. replace (/\ n [\ s |] * \ r/g, '\ n'); // remove unnecessary blank lines str = str. replace (// ig, ''); // remove return str ;}
For more articles about examples of custom sharing functions implemented based on thinkPHP, refer to PHP Chinese network!