PHP generated micro-signal QR code example

Source: Internet
Author: User
Through the background interface can get the small program any page of the QR Code, scan the QR code can be directly into the small program corresponding to the page. The official recommendation is to generate and use small code, which has a better level of identification. There are currently 3 interfaces to generate small code, developers can choose the appropriate interface according to their own needs.

Interface A: For business scenario interface addresses where the required number of codes is low:

Https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN

The parameters are as follows:

Note: The small code generated by this interface, permanent and effective, the number limit is described at the end of this article, please use with caution. After the user scans the code into the applet, it will go directly to the path corresponding to the page.

Interface B: For business scenarios where the required number of codes is very large or only used temporarily:

Https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN

The parameters are as follows:

Note: The small code generated by this interface is permanently valid and the number is temporarily unlimited. After the user scans the code into the applet, the developer needs to get the value of the scene field in the corresponding page and then make the processing logic. Use the following code to get the value of the scene field in the QR code. The debug phase can be simulated using the development tool's conditional compilation custom parameter scene=xxxx, and the parameter values of scene at the time of the development tool simulation need to be UrlEncode

This is the home page of the Jspage (  options) {    //options in the scene need to use decodeuricomponent to get to the generated QR code when the incoming SCENE
  var scene = decodeURIComponent (Options.scene)  })


Interface C: For business scenarios where the required number of codes is low:

Https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN

The parameters are as follows:

Note: The small program generated by this interface QR code, Permanent effective, the number limit see at the end of the description, please use carefully. After the user scans the code into the applet, it will go directly to the path corresponding to the page.

These interface post parameters need to be turned into a JSON string, which does not support form form submission. Interface a plus interface C, the total number of generated code is limited to 100,000, please call carefully.
Here is an example of interface B, the generation of two-dimensional code image and save the local server:
1. Get Access_token
Access_token is the global unique interface call credential, the developer calls each interface need to use Access_token, need to be properly saved. have done development and public number development, this is the most basic, here to obtain the method and the public number to obtain Access_token exactly the same, method see: get the basic interface credentials Access_token

2. Generate two-dimensional code

/** * Generate applet QR code * @param string $QR _path storage path, relative to the program root (for example:/public/qrcode/) * @param string $filename stored picture name (for example: Aaa.png)         * @param string $scene QR code scene value * @param string $page QR code jump page * @param string $expires _in qr code valid time * @return [type] [Description] */function create_qrcode ($qr _path, $filename, $scene, $page = ", $expires _in=7200) {if (Empty ($QR _path)) Return array (' status ' =>0, ' info ' = ' missing storage path '); if (empty ($filename)) return array (' status ' =>0, ' info ' = ' Please determine the stored image name '); if (empty ($scene)) return array (' status ' =>0, ' info ' = ' missing two-dimensional code scene value '); if (!is_dir ('. '). $QR _path)) {//./public/qrcode/mkdir (Iconv ("GBK", "UTF-8", '. '). $QR _path), 0777,true); } $file = $qr _path. $filename;///public/qrcode/aaa.png$protocol = (!empty ($_server[' HTTPS "]) && $_server[' HTTPS ']!== ' off ' | | $_server[' server_port '] = = 443)? "https://", "http://", $fileUrl = $protocol. $_server[' Http_host '). $file;//http://yourhost/public/qrcode/aaa.png$ Savepath = '. '. $file;//./public/qrcode/aaa.pngif (File_exists ($savePath) {//Current time-file creation time < Expiration time if ((Time ()-filemtime ($savePath)) < $expires _in) return array (' status ' =>1, ' info ' = > $fileUrl); } $accessToken = ' xxxxxxxxxxxxxxxxxxxxxx '; Get to the Access_token$url = ' https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token= '. $accessToken; $qrcode = The array (' scene ' = = $scene, ' width ' = = ', ' page ' = ' $page, ' auto_color ' + true); $result = Request ($url, True, ' POST ', Json_encode ($qrcode)); $errcode = Json_decode ($result, true) [' Errcode ']; $errmsg = Json_decode ($result, true) [' ErrMsg '];if ($errcode) return array (' status ' =>0, ' info ' = $errmsg); $res = File_put_contents ($savePath, $result);//will get the QR code picture stream saved to the picture file if ($res ===false) return array (' status ' =>0, ' Info ' = ' Generate QR code failed '); Return array (' status ' =>1, ' info ' = $FILEURL); return local picture Address}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.