The first step is to create a ticket:
Then we need to create post data like this:
{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}} or {"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "test"}}}
{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}}
The first parameter represents the valid time, and the second parameter is the temporary QR code The identity of the , is a constant, and the third parameter is the "scene_id" we want to put in
It should be noted here that scene_str is created in the form of a string.
The first step, we are going to get the ticket (here we are using string form!!!)
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$accessToken; $queryAction = 'POST'; $template = array(); if($type == 1){ $template['expire_seconds'] = $expireSeconds; $template['action_name'] = 'QR_STR_SCENE'; }else{ $template['action_name'] = 'QR_LIMIT_STR_SCENE'; } $template['action_info']['scene']['scene_str'] = $sceneStr; $template = json_encode($template,JSON_UNESCAPED_UNICODE);
$template will return the following values
{"expire_seconds":"2592000","action_name":"QR_STR_SCENE","action_info":{"scene":{"scene_str":"你好"}}}
In this way, we have created a string that meets the requirements, and then we only need to call the interface.
return Curl::callWebServer($queryUrl, $template, $queryAction);
What is written in my Curl::callwebserver method is to call the method of external url.
will return if correct
{"ticket":"gQFL8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyWHNFdjRPYU1kWm0xZXBPOGhyY0UAAgSZZeFaAwQAjScA","expire_seconds":2592000,"url":"http:\/\/weixin.qq.com\/q\/02XsEv4OaMdZm1epO8hrcE"}
We will get tickets.
The second step is to exchange the QR code according to the ticket. Remind that the ticket needs to be urlencode.
$queryUrl = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($ticket);//Encode the string in url $queryAction = 'GET'; $result = Curl::callWebServer($queryUrl, '', $queryAction, 0);
In this case, if the ticket is correct, the http return code is 200, which is an image that can be displayed or downloaded directly.
We can download the image locally:
$filename='./Public/qrcode/' .time().'.jpg'; file_put_contents($filename, $result);
Then it can be displayed on the page.