PHP实现获取“分享到朋友圈”按钮点击状态及自定义分享内容接口

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





2.获取jsapi_ticket以便获取微信分享权限,我百度下载了jssdk.php顺便改了一丢丢,但还是差不多

<?php
class 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'//分享图片
   });
 });

<?php
require_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,不知道谁写的,我百度的。。。其实算不算原创,我只不过是学习了。。。呵呵


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值