PHP获取小程序码,小程序带参数跳转
发表时间:2020-11-20
发布人:葵宇科技
浏览次数:40
PHP获取小程序码,小程序带参数跳转
获取小程序码首先需要获取Accesstoken ,下面是获取Accesstoken,注意超时时间所有我把时间往前提了一下,生成小程序码的时候先调取获取Accesstoken的方法
//获取accesstokn 并保存
public function getAccessToken(){
//$file_path = VENDOR_PATH.'wchat/access_token';
$file_path = '../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={$this->wxAppId}&secret={$this->wxAppSecret}";
$ret = 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 getQrCode($openid){
$this->getAccessToken();//更新accesstoken
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$this->access_token";//官方获取小程序码url
$data['page']='pages/index/index';//小程序的跳转页面地址
$data['scene'] = "$openid"; //请求参数
$res = httpUtil($url,json_encode($data),'POST');
if(!empty($res['errcode'])){
return false;
}
$file ="qrcode/$openid.jpg";
file_put_contents('./'.$file,$res);
if (file_exists($file)) {
//return $_SERVER ['HTTP_HOST'].'/'.$file;
return '/'.$file;
}else{
return false;
}
}