zoukankan      html  css  js  c++  java
  • php微信生成微信公众号二维码扫描进入公众号带参数

    <?php
    namespace appapimodel;
    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."
    ");
            fclose($logger);
        }
    
    }
    

      

  • 相关阅读:
    介绍一款jquery ui组件gijgo(含tree树状结构、grid表格),特点:简易、文档全清晰易懂、示例代码
    【未完待续】API接口
    表单中Readonly和Disabled的区别:readonly在get和post可传值到后端,disabled不可
    Newtonsoft.Json 转换DateTime类型为字符串时,串内部会有一个T。解决方案
    一种历史详细记录表,完整实现:CommonOperateLog 详细记录某用户、某时间、对某表、某主键、某字段的修改(新旧值
    js return falsee.preventDefault() 以及session
    bootstrape学习
    Redis的PHP操作手册
    PHP表单常用正则表达式(URL、HTTP、手机、邮箱等)
    大型网站架构演化
  • 原文地址:https://www.cnblogs.com/phper12580/p/12564198.html
Copyright © 2011-2022 走看看