当前位置:Gxlcms > php框架 > 基于thinkPHP实现的微信自定义分享功能示例

基于thinkPHP实现的微信自定义分享功能示例

时间:2021-07-01 10:21:17 帮助过:11人阅读

本文实例讲述了基于thinkPHP实现的微信自定义分享功能。分享给大家供大家参考,具体如下:

在许多大的网站我们都会看到点击分享就可以把数据分享到微信或QQ或其它的平台了,下面我们来看一段php版微信自定义分享代码,代码参考官方开发的没有任何问题.

分享需要认证微信订阅号或者服务号.

php 代码(thinkphp):

  1. $appid='xxx';
  2. $appsecret='xxxx';
  3. $timestamp = time();
  4. $noncestr = $this->getRandStr(15);
  5. // dump();
  6. $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='. $this->get_token($appid,$appsecret) .'&type=jsapi';
  7. $ret_json = $this->curl_get_contents($url);
  8. $ret = json_decode($ret_json);
  9. $ticket = $ret-> ticket;
  10. //var_dump($ret);
  11. $strvalue = 'jsapi_ticket='.$ticket.'&noncestr='.$noncestr.'×tamp='.$timestamp.'&url=http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  12. $signature = sha1($strvalue);
  13. $this->assign('timestamp',$timestamp);
  14. $this->assign('nonceStr',$noncestr);
  15. $this->assign('signature',$signature);
  16. function get_token($appid,$appsecret){
  17. if(S('access_token')) return S('access_token');
  18. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
  19. $ret_json = $this->curl_get_contents($url);
  20. $ret = json_decode($ret_json);
  21. if($ret -> access_token){
  22. S('access_token',$ret -> access_token,7200);
  23. return $ret -> access_token;
  24. }
  25. }
  26. function is_weixin(){
  27. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
  28. return true;
  29. }
  30. return false;
  31. }
  32. function getRandStr($length){
  33. $str = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  34. $randString = '';
  35. $len = strlen($str)-1;
  36. for($i = 0;$i < $length;$i ++){
  37. $num = mt_rand(0, $len);
  38. $randString .= $str[$num];
  39. }
  40. return $randString;
  41. }
  42. function curl_get_contents($url){
  43. $ch = curl_init();
  44. curl_setopt($ch, CURLOPT_URL, $url);
  45. curl_setopt($ch, CURLOPT_TIMEOUT, 1);
  46. curl_setopt($ch, CURLOPT_MAXREDIRS, 200);
  47. curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
  48. curl_setopt($ch, CURLOPT_REFERER, _REFERER_);
  49. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  50. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  51. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  52. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  53. $r = curl_exec($ch);
  54. curl_close($ch);
  55. return $r;
  56. }

js代码:需要引入:http://res.wx.qq.com/open/js/jweixin-1.0.0.js

  1. wx.config({
  2. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  3. appId: 'wxae7c36a1349c5868', // 必填,公众号的唯一标识
  4. timestamp: '{$timestamp}', // 必填,生成签名的时间戳
  5. nonceStr: '{$nonceStr}', // 必填,生成签名的随机串
  6. signature: '{$signature}',// 必填,签名,见附录1
  7. jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  8. });
  9. wx.ready(function(){
  10. wx.onMenuShareTimeline({
  11. title: '{$contentInfo.title}', // 分享标题
  12. link: window.location.href, // 分享链接
  13. imgUrl: 'http://'+window.location.host+'{$categoryInfo.image}', // 分享图标
  14. success: function () {
  15. // 用户确认分享后执行的回调函数
  16. //alert(1111);
  17. //fxfunc();
  18. },
  19. cancel: function () {
  20. // 用户取消分享后执行的回调函数
  21. //alert("您取消了分享");
  22. }
  23. });
  24. wx.onMenuShareAppMessage({
  25. title: '{$contentInfo.title}', // 分享标题
  26. desc: removeHTMLTag('{$contentInfo.content}'), // 分享描述
  27. link: window.location.href, // 分享链接
  28. imgUrl: 'http://'+window.location.host+'{$categoryInfo.image}', // 分享图标
  29. type: '', // 分享类型,music、video或link,不填默认为link
  30. dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
  31. success: function () {
  32. // 用户确认分享后执行的回调函数
  33. //fxfunc();
  34. },
  35. cancel: function () {
  36. //alert("您取消了分享");
  37. // 用户取消分享后执行的回调函数
  38. }
  39. });
  40. // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
  41. });
  42. function removeHTMLTag(str) {
  43. str = str.replace(/<\/?[^>]*>/g,''); //去除HTML tag
  44. str = str.replace(/[ | ]*\n/g,'\n'); //去除行尾空白
  45. //str = str.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
  46. str=str.replace(/ /ig,'');//去掉 
  47. return str;
  48. }

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP微信开发技巧汇总》、《PHP编码与转码操作技巧汇总》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

人气教程排行