微信公众平台生成带参数二维码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信公众平台生成带参数二维码相关的知识,希望对你有一定的参考价值。
<?php // +---------------------------------------------------------------------- // | 获取微信公众带参数二维码 // | Author: wzb [email protected] // +---------------------------------------------------------------------- namespace Mob\Controller; use Think\Controller; class BinController extends Controller{ private $appId; private $appSecret; private $type; // private $logo_img; /********************** * 微信获取 access_token *********************/ protected function _initialize($uid=0){ $config = api(‘Config/lists‘); C($config); //添加配置 $this->appId = C(‘WX_APPID‘); $this->appSecret = C(‘WX_SECRET‘); $this->type=0; $site_url = ‘./Uploads/images/‘; $store_path = date(‘Y‘).‘/‘.date(‘m‘).‘/‘.date(‘d‘) ; //分目录存放 zkf 2015-7-31 if(!is_dir($site_url.$store_path)) { @mkdir($site_url.$store_path, 0777 , true) ; } $this->logo_img = $site_url.$store_path.‘/user_uid‘.$uid.‘.png‘; } /********************** * 微信获取 access_token *********************/ private function getAccessToken() { $m_appact = M(‘Appact‘,‘ot_‘,DB_GY); $data = $m_appact -> where(array(‘appid‘=> $this ->appId)) -> field(‘access_token,expire_time‘) -> find(); if ($data[‘expire_time‘] < time()) { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"; $res = json_decode($this->httpGet($url)); $access_token = $res->access_token; if ($access_token) { $data[‘expire_time‘] = time() + 7000; $data[‘access_token‘] = $access_token; $r = $m_appact -> where(array(‘appid‘=> $this->appId)) -> find(); $save_arr = array(‘access_token‘=> $data[‘access_token‘],‘expire_time‘=> $data[‘expire_time‘]); $add_arr = array(‘appid‘=>$this->appId,‘access_token‘=> $data[‘access_token‘],‘expire_time‘=> $data[‘expire_time‘]); $r ? $m_appact -> where(array(‘appid‘=> $this->appId)) -> save($save_arr) : $m_appact -> add($add_arr); } } else { $access_token = $data[‘access_token‘]; } return $access_token; } /********************** * 获取微信二维码 *********************/ function get_erma(){ $access_token = $this->getAccessToken(); if(max(0,$this->$type) == 0){ //临时 $qrcode = array( "expire_seconds" => 1800, "action_name" => "QR_SCENE", "action_info" => array( "scene" => array( "scene_id" => 10000 ) ) ); }else{ //永久 $qrcode = array( "action_name"=> "QR_LIMIT_SCENE", "action_info" =>array( "scene"=>array( "scene_id"=> 1000 ) ) ); } //创建二维码ticket $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$access_token"; $result = $this->https_post($url,json_encode($qrcode)); $jsoninfo = json_decode($result, true); $ticket = $jsoninfo["ticket"]; // 下载二维码 $get_img_url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket); $imageInfo = $this->downloadImageFromWeiXin($get_img_url); $filename = $this->logo_img; $local_file = fopen($filename, ‘w‘); if (false !== $local_file){ if (false !== fwrite($local_file, $imageInfo["body"])) { fclose($local_file); } } } /********************** * 下载图片函数 *********************/ function downloadImageFromWeiXin($url){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_NOBODY, 0); //只取body头 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $package = curl_exec($ch); $httpinfo = curl_getinfo($ch); curl_close($ch); return array_merge(array(‘body‘ => $package), array(‘header‘ => $httpinfo)); } /********************** * POST 数据函数 *********************/ function https_post($post_url, $post_data){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $post_url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_POST, 1 ); curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl,CURLOPT_USERAGENT,"Mozilla/4.0"); $result = curl_exec($curl); $error = curl_error($curl); curl_close($curl); return $error ? $error : $result; } /********************** * GET 数据函数 *********************/ 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; } }
以上是关于微信公众平台生成带参数二维码的主要内容,如果未能解决你的问题,请参考以下文章