Home > Article > Backend Development > PHP generates QR code for WeChat applet with parameters
This article mainly introduces the QR code generated by PHP for WeChat applet with parameters. It has certain reference value. Now I share it with you. Friends in need can refer to it
The WeChat mini program officially opens three interfaces for creating QR codes, one of which is for generating QR codes, and the other is a sunflower-shaped mini program code. I use PHP to generate QR codes here.
This request is also very easy. The WeChat development documentation has a request interface:
You need to obtain the APPID and APPSECRET of your own mini program
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
access_token is only valid for 2 hours, so it is best to cache it to avoid repeated requests
You can build an array, Then convert it into json data and assign it to a variable
$param = json_encode(array("path"=>"pages/index/index?id=123","width"=> 150));
Then POST the data to the WeChat server in exchange for a QR code
$_SESSION['expires_in'])) { $json = httpRequest( $access_token ); $json = json_decode($json,true); // var_dump($json); $_SESSION['access_token'] = $json['access_token']; $_SESSION['expires_in'] = time()+7200; $ACCESS_TOKEN = $json["access_token"]; } else{ $ACCESS_TOKEN = $_SESSION["access_token"]; } //构建请求二维码参数 //path是扫描二维码跳转的小程序路径,可以带参数?id=xxx //width是二维码宽度 $qcode ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$ACCESS_TOKEN"; $param = json_encode(array("path"=>"pages/index/index?id=123","width"=> 150)); //POST参数 $result = httpRequest( $qcode, $param,"POST"); //生成二维码 file_put_contents("qrcode.png", $result); $base64_image ="data:image/jpeg;base64,".base64_encode( $result ); //把请求发送到微信服务器换取二维码 function httpRequest($url, $data='', $method='GET'){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_AUTOREFERER, 1); if($method=='POST') { curl_setopt($curl, CURLOPT_POST, 1); if ($data != '') { curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } } curl_setopt($curl, CURLOPT_TIMEOUT, 30); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); curl_close($curl); return $result; } ?>
1. Create a new qrcode.php
2. Copy the above code into it
3. Modify the APPID and APPSECRET
4. Visit qrcode.php
to generate a small program with parameters 2 QR code
#But this interface generates a small program QR code with a LOGO.
We want to generate a simple QR code for a small program without a logo. Is it okay?
There is no official way, but we can use a third-party interface to generate ordinary QR codes.
1. Decode first, decode the QR code of the mini program just generated, and obtain the URL
2. Use ordinary QR code to obtain the URL The QR code generation interface generates ordinary QR codes without LOGO
In fact, there are many libraries for decoding. I directly use the third-party JSSDK decoding here. It is free and requires application. The interface and appid and appkey
生成无LOGO二维码
nbsp;html>PHP生成微信小程序二维码 点击生成无LOGO二维码
![]()
The above is decoding implemented through js. The decoding still calls an image address of the QR code generated by php
The above is calling a local image, so This decoding program needs to be used in conjunction with the PHP code that generates the QR code for the mini program.
After decoding, a QR code without LOGO needs to be generated. For this, I only need to call an interface.
Interface: http://qr.liantu.com/api.php?...
Then the combination of generating small program code and generating code without LOGO QR code is:
$_SESSION['expires_in'])) { $json = httpRequest( $access_token ); $json = json_decode($json,true); // var_dump($json); $_SESSION['access_token'] = $json['access_token']; $_SESSION['expires_in'] = time()+7200; $ACCESS_TOKEN = $json["access_token"]; } else{ $ACCESS_TOKEN = $_SESSION["access_token"]; } //构建请求二维码参数 //path是扫描二维码跳转的小程序路径,可以带参数?id=xxx //width是二维码宽度 $qcode ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$ACCESS_TOKEN"; $param = json_encode(array("path"=>"pages/index/index?id=123","width"=> 150)); //POST参数 $result = httpRequest( $qcode, $param,"POST"); //生成二维码 file_put_contents("qrcode.png", $result); $base64_image ="data:image/jpeg;base64,".base64_encode( $result ); //把请求发送到微信服务器换取二维码 function httpRequest($url, $data='', $method='GET'){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_AUTOREFERER, 1); if($method=='POST') { curl_setopt($curl, CURLOPT_POST, 1); if ($data != '') { curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } } curl_setopt($curl, CURLOPT_TIMEOUT, 30); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); curl_close($curl); return $result; } ?>生成小程序官方二维码
"/>
生成无LOGO二维码
nbsp;html>PHP生成微信小程序二维码 点击生成无LOGO二维码
![]()
But two js libraries are needed for decoding
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
php Daniel shared: PHP code writing specifications, a comprehensive summary
The above is the detailed content of PHP generates QR code for WeChat applet with parameters. For more information, please follow other related articles on the PHP Chinese website!