直接代码:
function http_curl($url){
//获取imooc
//1.初始化curl
$ch = curl_init();
//2.设置curl的参数
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//3.采集
$output = curl_exec($ch);
//4.关闭
curl_close($ch);
//var_dump($output);
return $output;
} function getWxAccessToken(){
//1.请求url地址
$appid = '';//这个就不给你们看了
$appsecret = '';
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
//2初始化
$ch = curl_init();
//3.设置参数
curl_setopt($ch , CURLOPT_URL, $url);
curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//4.调用接口 
$res = curl_exec($ch);
//5.关闭curl
if( curl_errno($ch) ){
var_dump( curl_error($ch) );
}
$arr = json_decode($res, true);
curl_close( $ch );

var_dump( $arr );
return $arr;
}
//获取jsapi_ticket票据
    function getJsApiTicket(){
//如果session中保存有效的jsapi_ticket
//if($_SESSION['jsapi_ticket_expire_time']>time() && $_SESSION['jsapi_ticket']){   //}else{
$accessToken = "";
$url="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$accessToken."&type=jsapi";
$res=$this->http_curl($url);
$arr = json_decode($res,true);
var_dump( $arr );
$_SESSION['jsapi_ticket']=$arr['ticket'];
$_SESSION['jsapi_ticket_expire_time']=time()+7000;
   //}
 echo $arr['ticket'],'<br/>';
return $arr['ticket'];
    }    //获取随机码
    function getRandCode($num){
$array=array(
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9'
);
$tmpstr='';
$max=count($array);
for($i=1;$i<=$num;$i++){
$key=rand(0,$max-1);//'A' -> $array[0]
$tmpstr.=$array[$key]; }
return $tmpstr;
    }//分享朋友圈
function shareWx(){
//获取jsapi_ticket票据
$jsapi_ticket="";
        $timestamp=time();
        echo $timestamp,'<br/>';
$noncestr=$this->getRandCode(16);
echo $noncestr,'<br/>';
//获取动态url
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
     $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
     echo $url,'<br/>';
//获取signature
$signature="jsapi_ticket=".$jsapi_ticket."&noncestr=".$noncestr."&timestamp=".$timestamp."&url=".$url;
echo $signature,'<br/>';
$signature=sha1($signature);
echo $signature,'<br/>';
        $this->assign('name','');
$this->assign('timestamp',$timestamp);
$this->assign('noncestr',$noncestr);
$this->assign('signature',$signature);
        $this->display('./app/Tpl/Index/share.html'); }
<script type="text/javascript">
wx.config({
    debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
    appId: '', // 必填,公众号的唯一标识
    timestamp:'<{$timestamp}>' , // 必填,生成签名的时间戳
    nonceStr: '<{$noncestr}>', // 必填,生成签名的随机串
    signature: '<{$signature}>',// 必填,签名,见附录1
    jsApiList: [
     'onMenuShareTimeline',
     'onMenuShareAppMessage'
    ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function(){
 alert('准备好了');
   wx.onMenuShareTimeline({
    title: 'test01', // 分享标题
    link: 'www.imooc.com', // 分享链接
    imgUrl: 'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png', // 分享图标
    success: function () { 
        // 用户确认分享后执行的回调函数
    },
    cancel: function () { 
        // 用户取消分享后执行的回调函数
    }
});
 wx.onMenuShareAppMessage({
    title: 'fenxiang', // 分享标题
    desc: 'fenxiang test01', // 分享描述
    link: 'www.imooc.com', // 分享链接
    imgUrl: 'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png', // 分享图标
    type: 'link', // 分享类型,music、video或link,不填默认为link
    dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
    success: function () { 
        // 用户确认分享后执行的回调函数
        alert('成功');
    },
    cancel: function () { 
        // 用户取消分享后执行的回调函数
         alert('失败');
    }
});
 });
wx.error(function(res){
alert('错误');
        });
</script>