公众号开发【分享功能】
分享功能代码如下:
关联页面share.html
//获取JsApiTicket票据的方法
function getJsApiTicket(){
//如果session中保存
if($_SESSION[\’jsapi_ticket_expire_time\’]>time() && $_SESSION[\’jsapi_ticket\’]){
$jsapi_ticket=$_SESSION[\’jsapi_ticket\’];
}else{
//$access_token=$this->getWxAccessToken();因为获取失败 不好用 所以测试账号获得
$access_token=”12_EDjuJXo7AKFzpxOWcgis-F_EaXNT6ITQt6ZBbMgMUhuqySF-5aD5bUv-p1TPT63oTIy3uXSDVMDIbnrcpzdFXgfqxHGZbN2_-lzlZ8MKUQ3dskH3B7wpkr1aQymY459MjXYfioZJJW7FNYnLYMNjAHAIWX”;
$url=”https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=”.$access_token.”&type=jsapi”;
$res=$this->http_curl($url);
$jsapi_ticket=$res[\’ticket\’];
$_SESSION[\’jsapi_ticket\’]=$jsapi_ticket;
$_SESSION[\’jsapi_ticket_expire_time\’]=time()+7000;
}
return $jsapi_ticket;
}//getJsApiTicket end
//获取noncestr随机码
function getRandCode($num=16){
$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);
$tmpstr.=$array[$key];
}
return $tmpstr;
} //getRandCode end
//微信分享
function shareWx(){
//1.获取jsapi_ticket票据
$jsapi_ticket=$this->getJsApiTicket();
$timestamp=time();
$noncestr=$this->getRandCode();
$url=\’http://l1230205.applinzi.com/gzh/wx.php/Index/shareWx\’;
//2.获取signature
$signature=”jsapi_ticket=”.$jsapi_ticket.”&noncestr=”.$noncestr.”×tamp=”.$timestamp.”&url=”.$url;
$signature=sha1($signature);
$this->assign(\’name\’,\’imooc\’);
$this->assign(\’timestamp\’,$timestamp);
$this->assign(\’noncestr\’,$noncestr);
$this->assign(\’signature\’,$signature);
$this->display(\’share\’);
}//shareWx end