//获取小程序码
public function getQrCode(){
header('Content-Type: text/html; charset=utf-8'); //网页编码
$this->getAccessToken();
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$this->access_token";
$data['page']='pages/jihuo/index';//页面路径
$data['scene'] = '1';//自定义参数 只能是数字
$res = $this->httpUtil($url,json_encode($data),'POST');
if(!empty($res['errcode'])){
return $this->renderError($model->getError() ?: '下载失败');
}
$file ="../web/member/".$batch;
if (!is_dir($file)) {
mkdir($file,0700);
}
file_put_contents($file.'/'.$CardNumber.".jpg",$res);
if (file_exists($file)) {
return True;
}else{
return $this->renderError($model->getError() ?: '下载失败');
}
}
//获取accesstokn 并保存
public function getAccessToken(){
$file_path = '../source/application/common/access_token';
if(is_file($file_path)){
$connect = file_get_contents($file_path);
if($connect!=''){
$ret = json_decode($connect,true);
//已存在
if($ret['access_token'] && time()-$ret['time'] <7000){
$this->access_token = $ret['access_token'];
return true;
}
}
}
$wx_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的APPID&secret=你的secret";
$ret = $this->httpUtil($wx_url);
$ret = json_decode($ret,true);
if($ret['access_token']){
$data = array(
'access_token'=>$ret['access_token'],
'time'=>time()
);
file_put_contents($file_path, json_encode($data));
$this->access_token = $ret['access_token'];
return true;
}
}
public function httpUtil($url, $data = ''){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($data != '') {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
PHP 微信小程序太阳码
最新推荐文章于 2025-06-16 08:25:51 发布