php WeChat generates WeChat public account QR code scanning to enter the public account with parameters

In order to meet the needs of user channel promotion analysis and user account binding and other scenarios, 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, the official account can receive event pushes.

There are currently 2 types of QR codes:

1. Temporary QR codes have an expiration time. The longest can be set to expire after 30 days (ie 2,592,000 seconds) after the QR code is generated, but a larger number can be generated. Temporary QR codes are mainly used in business scenarios such as account binding that do not require permanent QR codes to be stored.
2. Permanent QR codes have no expiration time, but the number is small (currently up to 100,000). Permanent QR codes are mainly used in scenarios such as account binding and user source statistics.

When a user scans a QR code with a scene value, the following two events may be pushed:

If the user has not followed the official account, the user can follow the official account, and WeChat will push the following events with scene value to the developer after following the account.

If the user has followed the official account, it will automatically enter the session after the user scans, and WeChat will also push the scan event with scene value to the developer.

The process of obtaining a QR code with parameters includes two steps. First, create a QR code ticket, and then use the ticket to exchange for the QR code at the specified URL.

Create a QR code ticket

Each time you create a QR code ticket, you need to provide a parameter (scene_id) set by the developer, and introduce the process of creating a QR code ticket for a temporary QR code and a permanent QR code.

Note
expire_seconds The valid time of the QR code, in seconds. The maximum value does not exceed 2592000 (that is, 30 days). If this field is not filled in, the default validity period is 30 seconds.
action_name QR code type, QR_SCENE is a temporary integer parameter value, QR_STR_SCENE is a temporary string parameter value, QR_LIMIT_SCENE is a permanent integer parameter value, QR_LIMIT_STR_SCENE is a permanent string parameter value
action_info QR code details
scene_id scene value ID, a 32-bit non-zero integer for temporary QR codes, and a maximum value of 100000 for permanent QR codes (current parameters only support 1–100000)
scene_str Scene value ID (ID in string form), string type, length Limited to 1 to 64

<?php
namespace app\api\model;
set_time_limit(30);

class WxQrcode{
    //构造方法
    static $qrcode_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?";
    static $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&";
    static $qrcode_get_url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?";

    //生成二维码
    public function getEwm($fqid,$type = 1){
        $appid = '你的appid';
        $secret = '你的secret';
        $ACCESS_TOKEN = $this->getToken($appid,$secret);
        $url = $this->getQrcodeurl($ACCESS_TOKEN,$fqid,$type);
        save_log('测试保存的路径'.$url.'fid'.$fqid);
        return $this->DownLoadQr($url,time());
    }

    protected function getQrcodeurl($ACCESS_TOKEN,$fqid,$type = 1){
        $url = self::$qrcode_url.'access_token='.$ACCESS_TOKEN;
        if($type == 1){
            //生成永久二维码
            $qrcode= '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_str": '.$fqid.'}}}';
        }else{
            //生成临时二维码
            $qrcode = '{"expire_seconds": 604800, "action_name": "QR_STR_SCENE", "action_info": {"scene": {"scene_str": '.$fqid.'}}}';
        }
        $result = $this->http_post_data($url,$qrcode);
        $oo = json_decode($result[1]);
        if (empty($oo->ticket)){
            return false;
        }
        if(!$oo->ticket){
            $this->ErrorLogger('getQrcodeurl falied. Error Info: getQrcodeurl get failed');
            exit();
        }
        $url = self::$qrcode_get_url.'ticket='.$oo->ticket.'';
        return $url;
    }

    protected function getToken($appid,$secret){
        $ACCESS_TOKEN = file_get_contents(self::$token_url."appid=$appid&secret=$secret");
        $ACCESS_TOKEN = json_decode($ACCESS_TOKEN);
        $ACCESS_TOKEN = $ACCESS_TOKEN->access_token;
        return $ACCESS_TOKEN;
    }

    protected function http_post_data($url, $data_string) {

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($data_string))
        );
        ob_start();
        curl_exec($ch);
        if (curl_errno($ch)) {
            $this->ErrorLogger('curl falied. Error Info: '.curl_error($ch));
        }
        $return_content = ob_get_contents();
        ob_end_clean();
        $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        return array($return_code, $return_content);
    }

    //下载二维码到服务器
    protected function DownLoadQr($url,$filestring){
        if($url == ""){
            return false;
        }
        $filename = $filestring.rand(0,99999999999).'.jpg';
        ob_start();
        readfile($url);
        $img=ob_get_contents();
        ob_end_clean();
        $size=strlen($img);
        $fp2=fopen('static/qrcode/'.$filename,"a");
        if(fwrite($fp2,$img) === false){
            $this->ErrorLogger('dolwload image falied. Error Info: 无法写入图片');
            exit();
        }
        fclose($fp2);
        return 'static/qrcode/'.$filename;
    }

    //错误日志
    private function ErrorLogger($errMsg){
        $logger = fopen('log.txt', 'a+');
        fwrite($logger, date('Y-m-d H:i:s')." Error Info : ".$errMsg."\r\n");
        fclose($logger);
    }

}

Liao Shengping's blog, please
refer to the official WeChat documentation

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326082307&siteId=291194637