获取小程序码
接口地址:https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN
小程序中使用代码:
/获取access_token public function get_access_token(){ $appid = $wx_config['appid']; $secret = $wx_config['secret']; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}"; return $data = $this->curl_get($url); } public function curl_get($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($curl); $err = curl_error($curl); curl_close($curl); return $data; } //获得二维码 public function get_qrcode() { header('content-type:image/gif'); $uid = 6; $data = array(); $data['scene'] = "uid=" . $uid; $data['page'] = "pages/index/index"; $data = json_encode($data); $access = json_decode($this->get_access_token(),true); $access_token= $access['access_token']; $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token; $da = $this->get_http_array($url,$data); echo json_encode(array('pictures'=>$da)); $this->assign('data',$da); $this->fetch(); } public function get_http_array($url,$post_data) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //没有这个会自动输出,不用print_r();也会在后面多个1 curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $output = curl_exec($ch); curl_close($ch); $out = json_decode($output); return $out; } //微信小程序js文件中查看scene所带的参数 Page({ onLoad: function(options) { // options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene var scene = decodeURIComponent(options.scene) consol.log(scene) } }) //我这里传的参数为$data['scene'] = "uid=" 10086; //使用consol.log(scene);得到的结果为 uid=10086 //获得uid 的值 var uid = scene.split("=")[1];/
网页里使用:
类库下载地址:http://phpqrcode.sourceforge.net/
include 'phpqrcode.php'; $value = 'http://www.cnblogs.com/txw1958/'; //二维码内容 $errorCorrectionLevel = 'L';//容错级别 $matrixPointSize = 6;//生成图片大小 //生成二维码图片 QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2); $logo = 'logo.png';//准备好的logo图片 $QR = 'qrcode.png';//已经生成的原始圆形二维码图 if ($logo !== FALSE) { $QR = imagecreatefromstring(file_get_contents($QR)); $logo = imagecreatefromstring(file_get_contents($logo)); $QR_width = imagesx($QR);//圆形二维码图片宽度 $QR_height = imagesy($QR);//圆形二维码图片高度 $logo_width = imagesx($logo);//圆形logo图片宽度 $logo_height = imagesy($logo);//圆形logo图片高度 $logo_qr_width = $QR_width / 5; $scale = $logo_width/$logo_qr_width; $logo_qr_height = $logo_height/$scale; $from_width = ($QR_width - $logo_qr_width) / 2; //重新组合圆形图片并调整大小 imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height); } //输出图片 imagepng($QR, 'helloweixin.png'); echo '<img src="helloweixin.png" />';