后台:
public function share(){ $appId = C("WX_APPID"); //appid $appSecret = C("WX_APPSECRET");// 秘钥 $curl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}"; $ch = curl_init(); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_URL, $curl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); $res = curl_exec($ch);// 获得token curl_close($ch); $ress = json_decode($res,True); $token = $ress['access_token'];// 取出 至于存储代码就不列举了 $jsurl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$token."&type=jsapi"; $ch = curl_init(); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_URL, $jsurl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //$res = file_get_contents($curl);// 获得token $js = curl_exec($ch);// 获得token curl_close($ch); $jss = json_decode($js,True); $jsapi_ticket = $jss['ticket'];// 取出JS凭证, 至于存储代码就不列举了 $dataa['noncestr'] = 'sjijfdif'; //随意字符串 一会要传到JS里去.要求一致 $dataa['jsapi_ticket'] = $jsapi_ticket; $dataa['timestamp'] = time(); $dataa['url'] = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];//动态获取URL ksort($dataa); $signature = ''; foreach($dataa as $k => $v){ $signature .= $k.'='.$v.'&'; } $signature = substr($signature, 0, strlen($signature)-1); $signature = sha1($signature);// 必填,签名,见附录1 $this->assign(array( "appId"=>$appId, "timestamp"=>$dataa['timestamp'], "nonceStr"=>$dataa['noncestr'], "signature"=>$signature, )); $this->display();}
前台: