A thinkphp implementation based micro-letter custom Sharing Function Example _php instance

Source: Internet
Author: User

This article illustrates the micro-letter custom sharing feature based on thinkphp implementation. Share to everyone for your reference, specific as follows:

In many large sites we will see the click-Sharing to share the data to the micro-letter or QQ or other platforms, we look at a section of the PHP version of the micro-letter custom sharing code, code reference to the official development of the no problem.

Share requires authentication micro-credit subscription number or service number.

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 FA
Lse
 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,//Open debug mode, the return value of all invoked APIs will be on the client alert, to view incoming parameters, can be opened on the PC side, the parameter information will be typed through the log, only on the PC side will print.) AppId: ' wxae7c36a1349c5868 ',//required, public number unique identification timestamp: ' {$timestamp} ',//required, generate signature timestamp noncestr: ' {$nonceStr} ',//required, generate sign Random string Signature: ' {$signature} ',/must be filled, signed, see Appendix 1 jsapilist: [' onmenusharetimeline ', ' onmenushareappmessage ']//must fill,
Need to use the JS interface list, all JS interface list see appendix 2}); Wx.ready (function () {wx.onmenusharetimeline ({title: ' {$contentInfo. Title} ',//share title Link:window.location.href,//share chain Connect imgurl: ' http://' +window.location.host+ ' {$categoryInfo. Image} ',//Share icon Success:function () {//callback function executed after user confirms share//a
 Lert (1111);
 Fxfunc ();
 }, Cancel:function () {//The callback function executed after the user cancels sharing//alert ("You canceled sharing");
}
}); Wx.onmenushareappmessage ({title: ' {$contentInfo. Title} ',//share title Desc:removehtmltag (' {$contentInfo. Content} '),// Share description link:window.location.href,//Share link imgurl: ' http://' +window.location.host+ ' {$categoryInfo. Image} ',//share icon type: ",//share type, music, video or link, do not fill default for link Dataurl: ',//if type is music or video, provide a data link, default is the null success:function () {//user confirm share after the execution of the callback function//fxfunc ();
 }, Cancel:function () {//alert ("You canceled sharing");
 callback function to execute after user cancels sharing}); Config information is validated after the Ready method is executed, all interface calls must be the result of the Config interface, config is a client's asynchronous operation, so if you need to call the relevant interface when the page is loaded, The relevant interfaces must be called in the ready function to ensure proper execution.
For interfaces that are invoked when a user fires, they can be called directly and do not need to be placed in the ready function.
}); function Removehtmltag (str) {str = str.replace (/<\/?[ ^>]*>/g, ""); Remove HTML Tag str = str.replace (/[|) *\n/g, ' \ n '); Remove line end Blank//str = str.replace (/\n[\s| |) *\r/g, ' \ n ');
Remove excess empty line str=str.replace (/ /ig, ');/remove   return str;

 }

For more information on PHP related content readers can view the site topics: "PHP micro-credit Development Skills summary", "PHP coding and transcoding Operation skills Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP string (String) Usage Summary", "php+ MySQL Database operations Introduction tutorial and PHP Common database operation Skills Summary

I hope this article will help you with the PHP program design.

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.