modeljson
private $i = 0; public function getAccess_token($appid,$secret){ $reqUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}"; // crossDomain curl執行post請求,網上查找便可 $ref = crossDomain($reqUrl); $re = json_decode($ref,true); if($re['errcode'] != 0){ $this->i++; if($this->i < 4){ echo '請求失敗,稍後重試';die; } $this->getAccess_token($appid,$secret); } return $ref; }
public function getWxaCodeUnlimit($access_token){ $reqUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$access_token}"; return $reqUrl; }
controller小程序
public function qrCode(){ //格式自選,不一樣格式貌似加載速度略有不一樣,想加載更快可選擇jpg // header('content-type:image/gif'); // header('content-type:image/png'); header('content-type:image/jpg'); $ID = trim($this->input->get('ID')); if(!$ID || !is_numeric($ID)){ $errorArr = array('errcode'=>-3,'errmsg'=>'illegal request'); echo json_encode($errorArr,JSON_UNESCAPED_UNICODE); die; } $this->load->model('Wxapi_model', 'wxApi'); //小程序 $appId = '78897'; $appSecret = '78998645'; $access_token = $this->cache->get('access_token'); if(!$access_token){ $access_token = $this->wxApi->getAccess_token($appId,$appSecret); $this->cache->save('el:access_token',$access_token,(60*60*2-5)); } $access_token = json_decode($access_token,true); $getWxaCode = $this->wxApi->getWxaCodeUnlimit($access_token['access_token']); //參數 page =》 不是指服務端生成小程序碼接口,指的是小程序調用這個接口時的js文件路徑 $data = array( 'scene' =>'ID='.$ID, 'page' =>'pages/index/index', // 'page' =>'', 'width' =>20, // 'auto_color' =>false, ); $data = json_encode($data); //post curl執行post請求,網上查找便可 echo post($getWxaCode, $data); }