1. 程式人生 > >微信小程式二維碼獲取和儲存 PHP

微信小程式二維碼獲取和儲存 PHP

<?php class Wx { /* 請求地址 */ protected $url; /* 微信配置 */ protected $config; public function __construct(){ $this->url = 'https://api.weixin.qq.com/'; $this->config = array( 'appid' => '', 'mch_id' => '', 'AppSecret' => ''
, 'key' => '', 'notify_url' => '' ); } /** * 獲取微信token * @return boolean|mixed array('access_token'=>'ACCESS_TOKEN','expires_in'=>'有效時間','death_line'=>'失效時間戳') */ public function get_access_token(){ $token_cache = F('wx_token'
); /* 判斷快取token是否失效,未失效是直接使用快取的的token,已失效則重新獲取 */ if($token_cache['death_line']<=time()){ $ext_url = 'cgi-bin/token'; $grant_type = 'client_credential'; $appid = $this->config['appid']; $secret = $this->config['AppSecret']; $param
= array( 'grant_type'=>$grant_type, 'appid'=>$appid, 'secret'=>$secret, ); $full_url = $this->build_full_url($this->url.$ext_url, $param); $result = $this->curl_get($full_url); if($result!==FALSE){ $result = json_decode($result,true); /* 設定失效時間 */ $result['death_line'] = time() + $result['expires_in']; F('wx_token',$result); } }else{ $result = F('wx_token'); } return $result; } /** * 獲取小程式二維碼 * @param array $data 需要傳遞的引數,微信限制(最大32個可見字元,鍵名+鍵值+連線符['='|'&']<=32) * @param string $page 已經發布的小程式存在的頁面,根路徑前不要填加'/',不能攜帶引數(引數請放在scene欄位裡) * @param number $width 二維碼的寬度 * @param boolean $auto_color 自動配置線條顏色,如果顏色依然是黑色,則說明不建議配置主色調 * @param array $line_color auto_color 為 false 時生效,使用 rgb 設定顏色 例如 {"r":"xxx","g":"xxx","b":"xxx"} 十進位制表示 * @param boolean $is_hyaline 是否需要透明底色, is_hyaline 為true時,生成透明底色的小程式碼 * @return boolean|mixed */ public function get_mini_code($data,$page,$width=430,$auto_color=false,$line_color=array('r'=>'0','g'=>'0','b'=>'0'),$is_hyaline=false){ $token_info = $this->get_access_token(); $ext_url = 'wxa/getwxacodeunlimit'; $full_url = $this->build_full_url($this->url.$ext_url, array('access_token'=>$token_info['access_token'])); $wx_data = array( 'scene'=>'?'.http_build_query($data), 'page'=>$page, 'width'=>$width, 'auto_color'=>$auto_color, 'line_color'=>$line_color, 'is_hyaline'=>$is_hyaline, ); $wx_data = json_encode($wx_data); $res = $this->curl_post($full_url,$wx_data); return $res; } /** * * @param string $url * @param string $param * @return string */ public function build_full_url($url,$param){ $param_str = http_build_query($param); /* 判斷原連結是否已有引數 */ $joiner = strpos($string,'?')===false ? '?' : '&'; /* 拼接url */ $full_url = $url.$joiner.$param_str; return $full_url; } /** * 模擬GET請求 */ public function curl_get($url){ $httpInfo = array(); $ch = curl_init(); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_USERAGENT , 'limuzhengxin.com' ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 ); curl_setopt( $ch, CURLOPT_TIMEOUT , 60); curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true ); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt( $ch , CURLOPT_URL , $url); $response = curl_exec( $ch ); if ($response === FALSE) { return false; } $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE ); $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) ); curl_close( $ch ); return $response; } /** * 模擬POST請求 */ public function curl_post($url,$params=false){ $httpInfo = array(); $ch = curl_init(); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_USERAGENT , 'limuzhengxin.com' ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 ); curl_setopt( $ch, CURLOPT_TIMEOUT , 60); curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true ); curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt( $ch , CURLOPT_POST , 1 ); curl_setopt( $ch , CURLOPT_POSTFIELDS , $params ); curl_setopt( $ch , CURLOPT_URL , $url ); $response = curl_exec( $ch ); if ($response === FALSE) { return false; } $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE ); $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) ); curl_close( $ch ); return $response; } } /** * 獲取小程式二維碼並儲存到資料夾 * @param string $user_id 使用者ID * @return string 小程式二維碼儲存路徑 * @author lidong<[email protected]> */ function get_mini_code_path($user_id){ /* 小程式二維碼儲存目錄 */ $file_path = 'public/upload/user/mini_qrcode/'; mkdir_chmod($file_path); $filename = $file_path.$user_id.'.png'; if(!file_exists($filename)){ $Wx = new Wx(); $data=array( 'id'=>$goods_id, ); $page = ''; /* 小程式詳情頁 */ $res = $Wx->get_mini_code($data, $page); $im = file_put_contents($filename, $res); } $img = getHttpType().$_SERVER['HTTP_HOST'].'/'.$filename; return $img; } /** * 資料夾建立並且給許可權 * @param string $path 資料夾路徑 * @param string $mode 許可權,預設'0777' * @return bool * @author lidong<[email protected]> */ function mkdir_chmod($path, $mode = 0777){ if(is_dir($path)) { return true; } $result = mkdir($path, $mode, true); if($result) { $path_arr = explode('/',$path); $path_str = ''; foreach($path_arr as $val){ $path_str .= $val.'/'; $a = chmod($path_str,$mode); } } return $result; } /** * 獲取http協議型別 (http:// || https://) * * @return string(http:// || https://) * @author lidong<[email protected]> */ function getHttpType(){ $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://'; return $http_type; }