thinkPHP 微信生成带参数的二维码

//生成二维码(我使用的是phpqrcode生成二维码)

public function getUserQRcode($userid=0){
$qrurl=$this->getTicket($userid);
//echo $qrurl;
$url="http://你的域名/phpqrcode/index2.php?qrurl={$qrurl}";
$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);
$outopt = curl_exec($ch);
curl_close($ch);

//echo $outopt;

$userinfo=M("users")->where(array("userid"=>$userid))->find();
unlink($userinfo['qrcode']);
//更新二维码
$upuser["qrcode"]=$outopt;
$result=M("users")->where(array("userid"=>$userid))->save($upuser);

}

//获取生成带参数的二维码图片路径,然后把这个路径使用phpqrcode生成二维码

public function getTicket($userid=0){
$accessToken = $this->getAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$accessToken;
$data='{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": "'.$userid.'"}}}';
$res = $this->httpPost($url,$data);
//echo $res;
$result = json_decode($res, true);
return $result['url'];
//echo urldecode($result['url']);
}

public function httpPost($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}

//获取微信access_token

public function getAccessToken() {
// 如果是企业号用以下URL获取access_token
// $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".你的微信公众号APPID."&secret=".你的微信公众号APPSECRET;
$res = json_decode($this->httpGet($url));
$access_token = $res->access_token;
return $access_token;
}

public function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);


$res = curl_exec($curl);
curl_close($curl);


return $res;
}

phpqrcode.php代码自己从网上下载

phpqrcode代码index2.php

<?php    
include 'phpqrcode.php'; 

if(isset($_GET["qrurl"])&&$_GET["qrurl"]!=""){
$qrurl=$_GET["qrurl"];

//自动生成二维码

$remWord=date("YmdHis",time()).rand(1000,9999);

$value = $qrurl;//二维码内容
//$value = $remUrl;//二维码内容
$errorCorrectionLevel = 'L';//容错级别 
$matrixPointSize = 6;//生成图片大小 

//生成二维码图片 
QRcode::png($value, "qrupload/".$remWord.'.png', $errorCorrectionLevel, $matrixPointSize, 2); 
$logo = 'logo.jpg';//准备好的logo图片 
$QR = "qrupload/".$remWord.'.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 / 20; 
$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, "qrupload/".$remWord.'.png');

echo 'public/phpqrcode/qrupload/'.$remWord.'.png';

}

?>

猜你喜欢

转载自blog.csdn.net/u013239233/article/details/77675753
今日推荐