PHP实现获取“分享到朋友圈”按钮点击状态及自定义分享内容接口
来源:互联网 发布:数据库的主要逻辑模型 编辑:程序博客网 时间:2024/05/18 23:14
1.请参考我上个文章http://blog.csdn.net/wxs55555/article/details/72652058生成菜单
{ "button": [ { "type": "view", "name": "授权", "url": "http://161785yt21.51mypc.cn/wx.html" }, { "type": "view", "name": "分享", "url": "http://161785yt21.51mypc.cn/share.php" } ]}2.必须设置JS接口安全域名,不然将调不到微信js接口<script src='http://res.wx.qq.com/open/js/jweixin-1.0.0.js'></script>,也要设置网页授权获取用户基本信息,不然公众号无法进入你所在域名的url
<?phpclass JSSDK { private $appId; private $appSecret; public function __construct($appId, $appSecret) { $this->appId = $appId; $this->appSecret = $appSecret; } public function getSignPackage() { $urltemp="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appId."&secret=".$this->appSecret; $json=json_decode(file_get_contents($urltemp),true);$access_token=$json["access_token"];$urltemp="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi";$json=json_decode(file_get_contents($urltemp),true);$jsapiTicket=$json["ticket"]; // 注意 URL 一定要动态获取,不能 hardcode. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $timestamp = time(); $nonceStr = $this->createNonceStr(); // 这里参数的顺序要按照 key 值 ASCII 码升序排序 $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; $signature = sha1($string); $signPackage = array( "appId" => $this->appId, "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string ); return $signPackage; } private function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } private function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; }}
3.写分享页面share.php
必填参数:
<script src='http://res.wx.qq.com/open/js/jweixin-1.0.0.js'></script>
wx.config({
debug:true为调试模式,
appId:公众号appid,
timestamp:时间戳,
nonceStr:随机数,
signature:签名,
jsApiList: ['onMenuShareTimeline'] //需验证的接口,可写多个,这里只写了分享朋友圈,还有别的,可以百度
});
wx.ready(function () {//这里是网页加载完就自动生成,如果要绑定事件就要写事件
wx.onMenuShareTimeline({
title: '分享到朋友圈', //分享标题
desc:'吴小双测试',//分享描述
link:'http://161785yt21.51mypc.cn/share.php',//分享链接
imgUrl: 'http://161785yt21.51mypc.cn/img/share.jpg'//分享图片
});
});
<?phprequire_once "jssdk.php"; $jssdk = new JSSDK("此处填写测试公众号的appid", "此处填写测试公众号的appsecret");$signPackage = $jssdk->GetSignPackage();echo "<!DOCTYPE html><html lang='en'><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'><title>吴小双测试分享到朋友圈</title></head><body><h1>测试分享到朋友圈</h1></body><script src='http://res.wx.qq.com/open/js/jweixin-1.0.0.js'></script><script>wx.config({ debug: true, appId:'".$signPackage["appId"]."', timestamp:".$signPackage["timestamp"].", nonceStr:'".$signPackage["nonceStr"]."', signature:'".$signPackage["signature"]."', jsApiList: ['onMenuShareTimeline'] }); wx.ready(function () { wx.onMenuShareTimeline({ title: '分享到朋友圈', desc:'吴小双测试', link:'http://161785yt21.51mypc.cn/share.php', imgUrl: 'http://161785yt21.51mypc.cn/img/share.jpg' }); });</script></html>"?>
4.进入公众号点分享,分享菜单在第一步生成了
当wx.config中 debug: true的时候,调试页面,可弹出成功失败信息,线上就要设置为false
5完事了~~~~~,自己比较懒,不喜欢多写,如果觉得不充分请参考这个链接http://weixin.shinycg.com/php/sample.php,不知道谁写的,我百度的。。。其实算不算原创,我只不过是学习了。。。呵呵
- PHP实现获取“分享到朋友圈”按钮点击状态及自定义分享内容接口
- 微信JS-SDK实现自定义分享功能,分享给朋友,分享到朋友圈,后台实现
- 微信JS-SDK实现自定义分享功能,分享给朋友,分享到朋友圈
- 微信JS-SDK实现自定义分享功能,分享给朋友,分享到朋友圈
- PHP自定义实现微信分享朋友圈源码演示下载
- 分享内容到朋友圈微博等
- 微信分享到朋友圈接口用法
- 添加分享到朋友圈、发送给好友、分享等按钮
- 微信号开发自定义分享到朋友圈
- Android实现分享内容到微信朋友圈
- 微信自定义内容 分享到朋友圈成功后朋友看不到问题
- 分享图片到朋友圈
- 用友盟的组件实现分享内容到新浪微博,微信,朋友圈
- 屏蔽微信消息右上角的分享到朋友圈按钮
- 屏蔽微信消息右上角的分享到朋友圈按钮
- 微信如何分享朋友圈,php开发公众号分享到朋友圈
- PHP+MySQL微信通过jsapi分享到朋友圈等平台,实现设置图片、链接。
- 友盟自定义分享到朋友圈只显示图片
- 检查PHP脚本的执行效率(xdebug)
- Intellij IDEA 快捷键
- linux vps 挂载硬盘
- RSA加密与解密(比较详细))
- 数据/方法论固然重要,但人为分析更有价值!
- PHP实现获取“分享到朋友圈”按钮点击状态及自定义分享内容接口
- 利用Eclipse开发WSDL类型的webservice以及发布过程
- 《git-修改.gitignore后使其配置生效方法》---记录几个命令而已
- 设备模型的uevent机制
- Hibernate实体类创建注意事项
- Maven project .gitignore
- 求a[j]-a[i]的最大值,其中j>=i
- Moveit学习体会
- 利用加速度传感器模糊检测设备被人为移动的算法