PHP wechat applet interface: generate applet code, frequently asked questions

Posted by Mr Chew on Wed, 09 Feb 2022 21:06:58 +0100

1, Interface document address

Obtain applet code, which is applicable to business scenarios with a large number of codes. The small program code generated through this interface is permanently valid, and the number is unlimited.

2, Interface call

3, Return value

1. Explain

  • If the call succeeds, the binary content of the picture will be returned directly. If the request fails, the data in JSON format will be returned.

2. Normal return

  • Returned picture Buffer

3. Exception return

  • Data format: json
  • json content: errcode (number) - error code; errmsg (string) - error message
  • Legal value of errcode: 45009 - the call frequency is limited (currently 5000 times / minute, which will be adjusted). If a large number of small program codes are required, it is recommended to pre generate them; 41030 - the page passed does not exist or the applet is not published

4, Attention

  • POST parameters need to be converted into JSON strings, and form submission is not supported.
  • The interface can only generate the QR code of the published applet
  • The call frequency is limited (5000 times / minute). If a large number of small program codes are required, it is recommended to pre generate them
  • scene parameter: up to 32 visible characters, only numbers, upper and lower case English and some special characters are supported:! #$& ' ()*+,/:;=?@-._~, Other characters should be encoded as legal characters by yourself (because% is not supported, Chinese cannot be processed with urlencode, please use other encoding methods)
  • Page parameter: it must be the page existing in the published applet (otherwise an error will be reported), such as pages/index/index. Do not add /, and the parameter cannot be carried (please put the parameter in the scene field). If this field is not filled in, the main page will be skipped by default

5, PHP call

  • code
public function test() {
    $result = self::getQrCode();
    var_export($result);
    die();
}

//Get QR code
public function getQrCode() {
    $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN';
    $data = [
        'scene' => 'uid=1&orderId=2', //Maximum 32 visible characters, only numbers, upper and lower case English and some special characters are supported:! #$& ' ()*+,/:;=?@-._~, Other characters should be encoded as legal characters by yourself (because% is not supported, Chinese cannot be processed with urlencode, please use other encoding methods)
        'page' => '', //It must be the page where the published applet exists (otherwise an error will be reported). For example, pages/index/index, do not fill in /, and cannot carry parameters (please put the parameters in the scene field). If this field is not filled in, the main page will be skipped by default
        'width' => 200, //Width of QR code, in PX, minimum 280px, maximum 1280px
    ];
    $result = self::curlPost($url, $data);
    return $result;
}

    //json post request
public function curlPostJson($url, $data, $header = []) {
    $data = json_encode($data);
    $ch = curl_init();
    if (!empty($data)) {
        curl_setopt($ch, CURLOPT_POST,true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    $header[]='Content-Type: application/json; charset=utf-8';
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //If true, the redirected page will be tracked and crawled; otherwise, the redirected page will not be tracked
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); //Set timeout: 30s
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //Ignore ssl detection
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //1 or TRUE will curl_ The information obtained by exec () is returned as a string instead of being output directly-
    curl_setopt($ch, CURLINFO_HEADER_OUT, true); //When TRUE, trace the request string of the handle, which is available from PHP 5.1.3. The key is to allow you to view the request header
    curl_setopt($ch, CURLOPT_URL, $url);
    $output = curl_exec($ch);
    if (!$output) {
//        echo "request $url fail:", (array)curl_error($ch); // Log
    }
    curl_close($ch);
//    echo "request $url success:" . json_encode(array($url, $header, $output), true); // Log
    return $output;
}
  • output
'{"errcode":40001,"errmsg":"invalid credential, access_token is invalid or not latest rid: 60a3271a-770b3ab0-448314f5"}'

6, Frequently asked questions