微信分享封装
<?php class wxShare{ //获取wx.config需要的参数 public function getWxConfig($config,$link){ $access_token = $this->get_token($config['appid'],$config['appsecret'],$config['debug'],$config['noCacheToken']); $jsapiTicket = $this->get_ticket($access_token); $nonceStr = $this->nonceStr(16); $timestamp = time(); $string = 'jsapi_ticket='.$jsapiTicket.'&noncestr='.$nonceStr.'×tamp='.$timestamp.'&url='.$link; $signature = sha1 ($string); $WxConfig["appid"] = $config['appid']; $WxConfig["noncestr"] = $nonceStr; $WxConfig["timestamp"] = $timestamp; $WxConfig["signature"] = $signature; $WxConfig["debug"] = $config['debug']; $WxConfig["link"] = $link; return $WxConfig; } //生成签名的随机串 public function nonceStr($length){ $str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJK1NGJBQRSTUVWXYZ';//随即串,62个字符 $strlen = 62; while($length > $strlen){ $str .= $str; $strlen += 62; } $str = str_shuffle($str); return substr($str,0,$length); } //获取access_token public function get_token($appid,$appsecret,$debug=0,$noCacheToken=1){ if($_COOKIE['formplus_share_token'] == null || $debug == 1 || $noCacheToken == 1){ $result = $this->http_get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret); $result = json_decode($result,true); $access_token = $result['access_token']; setcookie('formplus_share_token',$access_token,time()+7199); }else{ $access_token = $_COOKIE['formplus_share_token']; } return $access_token; } //获取ticket public function get_ticket($access_token){ $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token='.$access_token; $res = json_decode($this->http_get($url)); $ticket = $res->ticket; return $ticket; } //请求方法 public function http_get($url){ $oCurl = curl_init(); if(stripos($url,"https://")!==FALSE){ curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1 } curl_setopt($oCurl, CURLOPT_URL, $url); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 ); $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); curl_close($oCurl); if(intval($aStatus["http_code"])==200){ return $sContent; }else{ return false; } } } ?>
引用
include_once('wxShare.class.php'); $wxShare = new wxShare(); //appid //appsecret //debug 开启调试模式 1或0 //noCacheToken 不缓存token,每次都重新获取 1或0 //$link 分享链接 $wxConfig = ['appid'=>'appid','appsecret'=>'appsecret','debug'=>'debug','noCacheToken'=>'noCacheToken']; $shareConfig = $wxShare->getWxConfig($wxConfig,$link);