Use to wechat interface is“ Generate QR code with parameters ”Two kinds of QR codes can be generated, one is temporary QR code, which will expire with a large amount of generation. It is mainly used in business scenarios such as account binding, where the QR code is not required to be permanently saved; the other is permanent QR code, which has no expiration time, but has a small amount of generation (currently up to 100000), which is mainly used in scenarios such as account binding, user source statistics, etc. After scanning, if the user does not pay attention to the official account, he will notice the concern. If he has noticed, he will go directly to the official account dialog box.
First, create a QR code ticket, and then use the ticket to the specified URL to exchange for QR code. For details, see the official documents https://developers.weixin.qq.com/doc/offiaccount/Account_Management/Generating_a_Parametric_QR_Code.html
Example code:
<?php namespace app\index\controller; use think\Controller; /** * WeChat class */ class Wechat extends Controller { protected $APPID = 'xxxxxxxxxxx'; protected $APPSECRET = 'xxxxxxxxxxxxxx'; /** * curl request */ public function http_curl($url, $type = 'get', $res = 'json', $arr = ''){ $cl = curl_init(); curl_setopt($cl, CURLOPT_URL, $url); curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, false); if($type == 'post'){ curl_setopt($cl, CURLOPT_POST, 1); curl_setopt($cl, CURLOPT_POSTFIELDS, $arr); } $output = curl_exec($cl); curl_close($cl); return json_decode($output, true); if($res == 'json'){ if( curl_error($cl)){ return curl_error($cl); }else{ return json_decode($output, true); } } } /** * Get AccessToken */ public function getAccessToken() { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->APPID."&secret=".$this->APPSECRET; // First judge access_token In file token Whether it is expired or not, continue to use if it is not expired, and update if it is expired $data = json_decode($this->get_php_file(ROOT_PATH."public".DS."wxtxt".DS."access_token.txt")); // Overdue updates if ($data->expire_time < time()) { $res = $this->http_curl($url); $access_token = $res['access_token']; if ($access_token) { // Add 7000 to the current timestamp s (Two hours) $data->expire_time = time() + 7000; $data->access_token = $res['access_token']; $this->set_php_file(ROOT_PATH."public".DS."wxtxt".DS."access_token.txt",json_encode($data)); } }else{ // Direct use without expiration $access_token = $data->access_token; } return $access_token; } // Get the token ticket private function get_php_file($filename) { return trim(file_get_contents($filename)); } // hold token ticket Store in file private function set_php_file($filename, $content) { $fp = fopen($filename, "w"); fwrite($fp, $content); fclose($fp); } /** * Generate QR code */ public function getQrcode(){ $token = $this->getAccessToken(); $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$token"; $param = array(); $param['action_name'] = "QR_LIMIT_SCENE"; $param['action_info'] = array( 'scene' => array( 'scene_id'=>'123' ) ); $param = json_encode($param); // Get QR code's ticket And QR code image resolution address $res = $this->http_curl($url, 'post', 'json', $param); // adopt ticket In exchange for QR code $qrcode = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$res['ticket'];
//Output QR code picture path
echo "<center><img src=".$qrcode."></center>"; } }
Run the getQrcode () method with the following effect