废话不多说,直接上代码
HTML代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- 必须引入的文件-->
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script src="/assets/p-default/js/jquery-1.7.2.min.js"></script>
</head>
<body id="weixinshare">
微信分享开发
<img style="width:672px; height:345px; cursor:pointer" alt="banner01" src="/assets/p-default/images/ldy/2.jpg">
</body>
</html>
<script type="text/javascript">
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: '235346457', // 必填,公众号的唯一标识
timestamp: "<?php echo $time?>", // 必填,生成签名的时间戳
nonceStr: "<?php echo $noncestr?>", // 必填,生成签名的随机串
signature: "<?php echo $signature?>",// 必填,签名
jsApiList: [
// 所有要调用的 API 都要加到这个列表中
'onMenuShareTimeline', // 分享到朋友圈接口
'onMenuShareAppMessage', // 分享到朋友接口
'onMenuShareQQ', // 分享到QQ接口
'onMenuShareWeibo' // 分享到微博接口
] // 必填,需要使用的JS接口列表
});
wx.ready(function(){
wx.onMenuShareAppMessage({
title: 'dabao87博客', // 分享标题
desc: 'sdfgdfghrtghtyj', // 分享描述
link: window.location.href, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: 'http://<?php echo $_SERVER['HTTP_HOST']?>/ldy/icon.png', // 分享图标
type: '', // 分享类型,music、video或link,不填默认为link
dataUrl: 'link', // 如果type是music或video,则要提供数据链接,默认为空
success: function () {
// 用户点击了分享后执行的回调函数
}
});
wx.onMenuShareTimeline({
title: 'dabao87博客', // 分享标题
link: window.location.href, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: 'http://<?php echo $_SERVER['HTTP_HOST']?>/ldy/icon.png', // 分享图标
success: function () {
// 用户点击了分享后执行的回调函数
},
});
});
wx.error(function(res){
// config信息验证失败会执行error函数,如签名过期导致验证失败,
// 具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,
//对于SPA可以在这里更新签名。
alert("好像出错了!!");
});
</script>
PHP代码
<?php
class Controller_Wxshare extends Controller{
public function action_index(){
$input = HttpIO::GET();
//下面的链接中的appid和是secret一定要填自己的
$access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=235346&secret=32454364";
// $access_msg = $this->doCurlGetRequest($access_token);
$access_msg = json_decode($this->httpGet($access_token));
$jsapi_ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_msg->access_token."&type=jsapi";
$this->session()->start();
$ticket = $_SESSION['ticket'];
if(!$ticket){
$jsapi_ticket_msg = json_decode($this->httpGet($jsapi_ticket_url));
$_SESSION['ticket'] = $jsapi_ticket_msg->ticket;
$ticket = $jsapi_ticket_msg->ticket;
}
$noncestr = $this->create_noncestr();
$time = time();
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$string = "jsapi_ticket=$ticket&noncestr=$noncestr×tamp=$time&url=$url";
$signature = sha1($string);
$view = new View('wxshare/index');
$view->time = $time;
$view->noncestr = $noncestr;
$view->signature = $signature;
$view->css_list = $css_list;
$view->render();
}
private function create_noncestr($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 create_signature($nocestr,$ticket,$timestamp,$url){
$signature = "";
// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$s = "jsapi_ticket=" . $ticket . "&noncestr=" . $nocestr . "×tamp=" . $timestamp . "&url=" . $url;
return sha1($s);
}
private function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
// 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
// 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
}
官方有文档DEMO:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115,附录5