Home  >  Article  >  Backend Development  >  WeChat custom sharing php code analysis_php example

WeChat custom sharing php code analysis_php example

WBOY
WBOYOriginal
2016-12-05 13:28:291947browse

I did custom sharing on WeChat some time ago and accumulated some experience to share with everyone.


Steps 1 and 2 are already very detailed in the WeChat developer documentation, just start from step three.

Step 3To parameterize WeChat parameters, except appId, other parameters are dynamic. Here, I use PHP language to generate parameters in the backend. The code is as follows:
PHP side code:

public function getwxinfo(){ 
 $dataurl = I('dataurl'); 
 include('./ThinkPHP/Library/Vendor/jssdk/jssdk.php'); 
 $jssdk = new \JSSDK('appId','appSecret'); 
 $signPackage = $jssdk->GetSignPackage($dataurl); 
 exit(response('1','ok',$signPackage)); 
} 

When generating a signature, the URL of the current page must be obtained, so $dataurl is dynamically obtained through the following JS, remember.
JS side code:

var dataurl = window.location.href; 
$.ajax({ 
 type:'post', 
 data:{'dataurl':dataurl}, 
 url:'/index.php/Home/Index/getwxinfo', 
 dataType:'json', 
 success:function(json){ 
  var list = json.data; 
  var appId = list.appId; 
  var timestamp = list.timestamp; 
  var nonceStr = list.nonceStr; 
  var signature = list.signature; 
  weixinInfo(appId,timestamp,nonceStr,signature); 
 }, 
 error:function() { 
  //alert('网络不给力哦...') 
 } 
}); 
function weixinInfo(appId,timestamp,nonceStr,signature){ 
 wx.config({ 
  debug: false, 
  appId: appId, 
  timestamp:timestamp , 
  nonceStr:nonceStr, 
  signature:signature, 
  jsApiList: [ 
   'checkJsApi', //判断当前客户端版本是否支持指定JS接口 
   'onMenuShareTimeline', //分享到朋友圈 
   'onMenuShareAppMessage', //分享给好友 
   'onMenuShareQQ', //分享到QQ 
   'onMenuShareWeibo' //分享到微博 
  ] 
 }); 
} 
 
wx.ready(function () { 
 //微信好友 
 var s_title = '分享标题'; 
 var s_desc = '分享描述'; 
 var s_link = '分享后的链接'; 
 var s_imgUrl = '图片链接'; 
 wx.onMenuShareAppMessage({ //例如分享到朋友圈的API 
  title: s_title, // 分享标题 
  desc: s_desc, // 分享描述 
  link: s_link, // 分享链接 
  imgUrl: s_imgUrl, // 分享图标 
  success: function () { }, 
  cancel: function () { } 
 }); 
 //朋友圈 
 wx.onMenuShareTimeline({ 
  title: s_title, // 分享标题 
  link: s_link, // 分享链接 
  imgUrl: s_imgUrl, // 分享图标 
  success: function () { }, 
  cancel: function () { } 
 }); 
 //QQ好友 
 wx.onMenuShareQQ({ 
  title: s_title, // 分享标题 
  desc: s_desc, // 分享描述 
  link: s_link, // 分享链接 
  imgUrl: s_imgUrl, // 分享图标 
  success: function () { }, 
  cancel: function () { } 
 }); 
}); 
wx.error(function (res) { <br />alert(res.errMsg); //打印错误消息。及把 debug:false,设置为debug:ture就可以直接在网页上看到弹出的错误提示 <br />}); 

It’s that simple.

The above is the entire content of this article. I hope it will be helpful to everyone’s study. I also hope that everyone will support Script Home.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn