生成微信二维码

//生成二维码
public function my_ewm()
{
$file = file_get_contents("php://input");
$code = json_decode($file, true);
$appid = 'wx1a8b582067ebb8a5';
$appsecret = '08fb395003441b482583b218d306280b';//appid对应的appsecret
header('content-type:text/html; charset=utf-8');
//$_POST=$_REQUEST;
$openid = $code['openid'];
$user = M("user")->where(["openid" => $openid])->find();
if (!$openid) {
$data = array("result" => '2', "message" => "参数有误!"); //组装JSON
echo json_encode($data); //返回
die;
}
$goodsid = $user['id'];
if (!$goodsid) {
echo "空的id!";
die;
}
$filename = $goodsid . '.jpg';
$file = './Public/qrcode/' . $filename;
$scene = "?scene=" . $goodsid;
if (file_exists($file)) {
echo "<script>location.href='http://zgmm.a40.com.cn/Public/qrcode/" . $filename . "" . $scene . "'</script>";
} else {
//请求accesstoken接口
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;
$https = true;
$method = 'get';
$data = null;
//get curl传值
$str = $this->get_curl($url, $data, $method);
//获取请求到的结果讲上面获取的accresstoken进行解码成数组,然后获取accesstoken值
$arr = json_decode($str, true);
// dump($arr);die;
$access_token = $arr['access_token'];
//获取二维码,但是切记这里accesstoken不能用通过post传值,只能用get传值
$width = 430;
$url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=' . $access_token;
//$data['scene']=$goodsid;//"t_id=".$userinfo['id'];
//$data['path']='pages/home/homedetail';
//$data['width']=$width;
$data['path'] = 'pages/saoma/saoma?id=' . $goodsid;
$data['width'] = $width;
$post_data = json_encode($data);
$https = true;
$method = 'post';
//post curl传值
$str = $this->get_curl($url, $post_data, $method);
//进行保存
$res = file_put_contents($file, $str);
//如果保存成功返回路径
if ($res != 0) {
echo "<script>location.href='http://zgmm.a40.com.cn/Public/qrcode/" . $filename . "" . $scene . "'</script>";
} else {
echo "生成小程序码出错!";
}
}

}












/****我的二维码*******//生成二维码【邀请人的二维码】
public function share_ewm(){
header('content-type:text/html; charset=utf-8');
//$_POST=$_REQUEST;
$uid=$_POST['uid'];
if(!$uid){
$this->share_ewm_1();
/*$data = array("result" =>'2',"message"=>"参数有误!"); //组装JSON
echo json_encode($data); //返回
die;*/
}else{
$userinfo=M('user')->where(array('id'=>$uid,'is_xs'=>1))->find();
if(!$userinfo['qrcode']){
$appid='wx010bce04be848770';
$appsecret='009a404dc28e957c8ca27b82322850f9';//appid对应的appsecret
//请求accesstoken接口
$url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
$https=true;
$method='get';
$data=null;
//get curl传值
$str=$this->get_curl($url,$data,$https,$method);
//获取请求到的结果讲上面获取的accresstoken进行解码成数组,然后获取accesstoken值
$arr = json_decode($str,true);
// dump($arr);die;
$access_token=$arr['access_token'];
//获取二维码,但是切记这里accesstoken不能用通过post传值,只能用get传值
$path="./Public/qrcode/";
/*$width=200;
$url='https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;
$data['scene']=$userinfo['id'];//"t_id=".$userinfo['id'];
$data['path']='pages/Testing/Testing';
$data['width']=$width;
$post_data=json_encode($data);
$https=true;
$method='post';
//post curl传值
$str=$this->get_curl($url,$post_data,$https,$method);*/
$path = 'pages/Testing/Testing?scene='.$userinfo['id'];
$width = 430;
$str = $this->getWXACodeUnlimit($access_token,$path,$width);
//获取当前时间措
$time=time();
//指定文件保存路径
$file = "./Public/qrcode/".$time.$userinfo['id'].'.jpg';
//进行保存
$res=file_put_contents($file, $str);
//如果保存成功返回路径
if($res!=0){
$save_ewm['qrcode']=$file;
$yesOrno =M("user")->where(array("id"=>$userinfo['id']))->save($save_ewm);
if($yesOrno){
$infos = M("user")->where(array('id'=>$userinfo['id']))->field('id,pic,uname,qrcode')->find();
$infos['qrcode']=trim($infos['qrcode'],'.');
}
//return json_encode($infos,JSON_UNESCAPED_UNICODE);
}
}else{
$infos = M("user")->where(array('id'=>$userinfo['id']))->field('id,pic,uname,qrcode')->find();
$infos['qrcode']=trim($infos['qrcode'],'.');
}
//最后一次的检测结果
$log_test['uid']=$infos['id'];
$log_testxx= M("user")->where($log_test)->order('id desc')->select();
$log_info= M("test_log")->where(array("id"=>$log_testxx[0]['id']))->find();

$data = array("result" =>'1',"message"=>"成功!","qrcode"=>$infos['qrcode'],'lists'=>$infos,"test_log"=>$log_info); //组装JSON
echo json_encode($data); //返回
die;
}
}










posted @ 2020-09-15 11:32  php毛宏历的博客  阅读(298)  评论(0编辑  收藏  举报