zoukankan      html  css  js  c++  java
  • 这是一个微信带参数的二维码,自定义菜单,与图文回复

    <?php
    namespace appmbcontroller;
    
    use thinkController;
    use thinkRequest;
    use thinkDb;
    use appmbmodelWeixin;
    
    class Qr extends Controller
    {
        public function sdkjoin()
        {
            /* $data = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER["QUERY_STRING"];
            $data = ['test' =>$data];
            Db::table('weixin')->insert($data); */
            /* $nonce     = $_GET['nonce'];
            $token     = 'sdkjoinasddfg';
            $timestamp = $_GET['timestamp'];
            $echostr   = $_GET['echostr'];
            $signature = $_GET['signature'];
            $array = array($token, $timestamp, $nonce);
            sort($array, SORT_STRING);
            $str = implode($array);
            if(sha1($str)==$signature)
            {
                echo $echostr;
            } */
            
            
            $postStr = file_get_contents("php://input");
            $data = ['test' =>$postStr];
            Db::table('weixin')->insert($data);
            if (! empty ( $postStr ))
            {
                
                $postObj = simplexml_load_string ( $postStr, 'SimpleXMLElement', LIBXML_NOCDATA );
                //click
                if($postObj->Event == 'CLICK')
                {
                    $fromUsername = $postObj->FromUserName;
                    $toUsername = $postObj->ToUserName;
                    $time = time();
                    $keyword = trim($postObj->EventKey);
                    $text = 
                    "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[%s]]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    </xml>";
                    $resultStr = sprintf($text,$fromUsername,$toUsername,$time,'text','xxxxxxxxx');
                    exit($resultStr);
                }
                //扫码
                if($postObj->Event == 'subscribe' || $postObj->Event == 'SCAN')
                { 
                    $fromUsername = $postObj->FromUserName;
                    $key = $postObj->EventKey;
                    $key = str_replace("qrscene_",'',$key);
                    $res = Db::table('sign')->where('openid',"{$fromUsername}")->find();
                    if(!$res)
                    {
                        $data = ['openid'=>"{$fromUsername}",'pid'=>$key,'timeline'=>time()];
                        Db::table('sign')->insert($data);
                    }
                //
                       $toUser = $postObj->FromUserName;
                       $fromUser = $postObj->ToUserName;
                       $time = time();
                       $msgType = 'news';
                       $arr=array(
                           array(
                               'title'=>'xxxxxxxxxx',
                               'description'=>'xxxxxxxx',
                               'picUrl'=>'https://xxxxxxxx.png',  //获取图片的路径
                               'url'=>'https://xxxxxxxxxx.php'    //点击跳转后的路径
                           ),
                           //进行多图文发送时,子图文个数不能超过10个
                       );
                       $template = "<xml><ToUserName><![CDATA[%s]]></ToUserName>
                                         <FromUserName><![CDATA[%s]]></FromUserName>
                                         <CreateTime>%s</CreateTime>
                                         <MsgType><![CDATA[%s]]></MsgType>
                                         <ArticleCount>".count($arr)."</ArticleCount>
                                         <Articles>";
                       foreach ($arr as $k=>$v){
                           $template.="<item><Title><![CDATA[".$v['title']."]]></Title> 
                                         <Description><![CDATA[".$v['description']."]]></Description>
                                         <PicUrl><![CDATA[".$v['picUrl']."]]></PicUrl>
                                         <Url><![CDATA[".$v['url']."]]></Url>
                                       </item>";
                       }
                       $template.=      "</Articles>
                                         </xml>";
                       $info = sprintf($template,$toUser,$fromUser,$time,$msgType);
                       echo $info;
                }//
            }
        }
        public function test()
        {
            $id = session('userid');
            if(!$id)
            {
                $this->redirect('mb/Register/login');
            }
            $userData = Db::table('xxxx')->where('id',$id)->find();
            $userData = $userData['id'];
            $wx = new Weixin;
            $token = $wx->center();
            $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={$token}";
            $array = array(
                                'action_name'=>'QR_LIMIT_SCENE',
                                'action_info'=>array(
                                        'scene'=>array('scene_id'=>$userData),
                                ),
                        );
            $param = json_encode($array);
            $res = $this->https_post($url,$param);
            $info = json_decode($res,true);
            $ticket = $info['ticket'];
            db('user')->where('id',$id)->update(['user_qr' =>$ticket]);
            $url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={$ticket}";
            $imgres = $this->getimg($url,$userData);
            db('user')->where('id',$id)->update(['imgpath' =>$imgres,'img_flag'=>1]);
        }
        
        public function https_post($url,$data = null)
        {
            $curl = curl_init();
            curl_setopt($curl,CURLOPT_URL,$url);
            curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
            curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);
            if(!empty($data))
            {
                curl_setopt($curl,CURLOPT_POST,1);
                curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
            }
            curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
            $output = curl_exec($curl);
            curl_close($curl);
            return $output;
        }
    
        public function setmenu()
        {
            //$token=session('wxtoken');
            $wx = new Weixin;
            $token = $wx->center();
            $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$token;
            $data=array(
                'button'=>
                [
                    [
                        'name'=>'xxxxx',
                        'sub_button'=>[
                            [
                                "type"=>'view',
                                'name'=>'xxxxxx',
                                'url'=>'https://xxxxxxxxx'
                            ],
                            [
                                "type"=>'view',
                                'name'=>'xxxxxxxx',
                                'url'=>'xxxxxxxxx'
                            ]
                        ]
                    ],
                    [
                        'name'=>'xxxxxx',
                        'sub_button'=>[
                            [
                                "type"=>'view',
                                'name'=>'xxxxxx',
                                'url'=>'xxxxxxxx'
                            ],
                            [
                                "type"=>'view',
                                'name'=>'xxxxx',
                                'url'=>'xxxxxxx'
                            ]
                        ]
                    ],
                    [
                        'name'=>'xxxxxx',
                        'type'=>'click',
                        'key'=>'xxxxx'
                    ]
                ]
            );
            $data=json_encode($data,JSON_UNESCAPED_UNICODE);
            $result=$this->get_http($url,$data);
            return $result;
        }
    
        private function get_http($url,$data)
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            // post数据
            curl_setopt($ch, CURLOPT_POST, 1);
            // post的变量
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $output = curl_exec($ch);
            curl_close($ch);
    
            //打印获得的数据
            return $output;
        }
        public function getimg($url,$imgname)
        {
            $image_content = file_get_contents($url);
            $http_type = get_headers($url);
            $result = explode('/',$http_type[3]);
            $type = '.png';
            $path_time = date('Ym');
            $path = "/var/www/xxxxxx/images/{$path_time}/";
            if (!file_exists($path)) 
            {
                mkdir ($path,0777,true );
            }
            file_put_contents($path.$imgname.$type,$image_content);
            return "/style/images/".$path_time."/".$imgname.$type;
        }
        
    }

     获取保存token

    <?php
    namespace appmbmodel;
    use thinkModel;
    use thinkDb;
    
    class Weixin extends Model
    {
        public function center()
        {
            return $this->getAccessToken();
        }
        
        //获取 token
        private function getAccessToken()
        {
            $appid = Config('appid');
            $secret = Config('secret');
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
            $data = Db::table('token')->where('id',2)->find();
            if($data)
            {
                $tokenArr = unserialize($data['token']);
                if(time() < $tokenArr['expire'])
                {
                    goto a;
                }
                else
                {
                    $tokenArr = json_decode($this->httpGet($url),true);
                    $tokenArr['expire'] = time()+7000;
                    $tokenStr = serialize($tokenArr);
                    Db::table('token')->where('id', 2)->update(['token' => $tokenStr]);
                }
            }
            else
            {
                $tokenArr = json_decode($this->httpGet($url),true);
                $tokenArr['expire'] = time()+7000;
                $str = serialize($tokenArr);
                $arr = ['token' => $str];
                $final = Db::table('token')->insert($arr);
            }
            a:
            return $tokenArr['access_token'];
        }
        
        private function httpGet($url)
        {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_TIMEOUT, 500);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
            curl_setopt($curl, CURLOPT_URL, $url);
            $res = curl_exec($curl);
            curl_close($curl);
            return $res;
        }
    }
  • 相关阅读:
    jsonp解决跨域
    rkhunter
    freshclam
    ntpdate
    一个汉字占几个字节
    plsql developer 使用 oracle instantclient的安装和配置
    初学者学习计划
    pslq常用操作
    plsql使用
    Tomcat性能调优方案
  • 原文地址:https://www.cnblogs.com/zhangxiangdong/p/8617921.html
Copyright © 2011-2022 走看看