Home  >  Article  >  Backend Development  >  How to generate QR code with parameters through WeChat interface

How to generate QR code with parameters through WeChat interface

小云云
小云云Original
2018-03-20 15:44:333391browse

In order to meet the needs of user channel promotion analysis, the public platform provides an interface for generating QR codes with parameters. Using this interface, multiple QR codes with different scene values ​​can be obtained. After the user scans them, the public account can receive event push.

When a user scans a QR code with a scene value, the following two events may be pushed:
(1) If the user has not followed the official account, the user can follow the official account and follow it on WeChat Attention events with scene values ​​will be pushed to developers.
(2) If the user has followed the official account, the user will automatically enter the session after scanning, and WeChat will also push the scanning event with scene value to the developer.

The process of obtaining a QR code with parameters includes two steps: (I) First create a QR code ticket; (II) Then use the ticket to the specified URL to exchange for the QR code.

(I) First create a QR code ticket
Every time you create a QR code ticket, you need to provide a parameter (scene_id) set by the developer.

/**
 * 首先创建二维码ticket   
 * @param string $sceneid 场景值ID
 * @param string $type   值为'temp'的时候生成临时二维码
 * @param string $expire_seconds   二维码过期时间
 * @return string 二维码ticket 
 */
public function _getTicket($sceneid,$type='temp',$expire_seconds=604800){
	if($type=='temp'){
		$data = '{"expire_seconds": %s, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": %s}}}';
		$data = sprintf($data,$expire_seconds,$sceneid);
	}else{
		$data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": %s}}}';
		$data = sprintf($data,$sceneid);
	}
	$curl = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$this->_getAccessToken();
	$content = $this->_request($curl,true,'POST',$data);
	$cont = json_decode($content);
	
	return $cont->ticket;
}


(II) Then use the ticket to the specified URL to exchange for the QR code
After obtaining the QR code ticket, the developer can use the ticket to exchange for the QR code image. Please note that this interface can be called without logging in.

//然后凭借ticket到指定URL换取二维码
public function _getQRCode($sceneid,$type='temp',$expire_seconds=604800){
	$ticket = $this->_getTicket($sceneid,$type,$expire_seconds);
	$curl = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($ticket);
	$content = $this->_request($curl);
	return $content;
}

Call output:

header('Content-type:image.jpeg');
$result = $wechat->_getQRCode(30);
echo $result;

The browser can directly output the QR code, which is a picture that can be displayed or downloaded directly.

Related recommendations:

Force.com WeChat development generates QR code with parameters

WeChat development generates QR code with parameters Examples of QR codes

WeChat development - Php batch generation of QR codes with parameters, php parameters_PHP tutorial

The above is the detailed content of How to generate QR code with parameters through WeChat interface. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn