thinkphp5 获取带参数公众号二维码 以及如何接收二维码参数 微信公众号开发(一)

   // 获取公众号带参数二维码
    public function getqrcodeticket($value='')
    {
        // @header('Content-type: text/plain;charset=UTF-8');
       // header("Access-Control-Allow-Origin: *");
        // request()->header('user-agent');
        //公众号 appid 和秘钥AppID 和 AppSecret 可登录微信公众平台官网-设置-开发设置中获得(需要已经绑定成为开发者,且帐号没有异常状态)

//获取access_token
        $appid = 'XXX';
        $appsecret = 'XXX';
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
        $res = httpGet($url);
        $res = json_decode($res);

      //组装二维码参数
        $title =‘公众号 lemingyue';
        $data['action_name'] ="QR_STR_SCENE";//有效期        QR_LIMIT_STR_SCENE 永久
        $data['expire_seconds'] ="2592000"; //时间
        $data['action_info']=array("scene_str"=>$title);
        $data = json_encode($data);
      // 获取ticket
        $urls = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=". $res->access_token;
        $result =$this->https_request($urls, $data);
        $result= json_decode($result);
        // dump($result);

     //获取二维码
        $urlss= "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($result->ticket);
        // $ress = httpGet($urlss);
        // header("location: $urlss") ;
        $this->ajaxReturn($urlss);  //前台用img 标签就可以显示图片//或者直接打开在浏览器 $urlss  地址
    }

 

//微信配置的服务器的地址

  public function _initialize()
    {
        parent::_initialize();
        $this->wchat = new WchatOauth(); // 微信公众号相关类
        $this->instance_id = 0;
        $wx_user = db('wx_user')->find();
        $this->token = $wx_user['token'];
        define("TOKEN", $this->token);
        $this->getMessage();
    }

public function getMessage()
    {
        $from_xml = file_get_contents('php://input');
        if (empty($from_xml)) {
            return;
        }
        $signature = input('msg_signature', '');
        $signature = input('timestamp', '');
        $nonce = input('nonce', '');
        $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];
        $ticket_xml = $from_xml;
        $postObj = simplexml_load_string($ticket_xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $this->instance_id = 0;

        if (!empty($postObj->MsgType)) {
            switch ($postObj->MsgType) {
                case "text":
                    //用户发的消息   存入表中
                    //$this->addUserMessage((string)$postObj->FromUserName, (string) $postObj->Content, (string) $postObj->MsgType);
                    $resultStr = $this->MsgTypeText($postObj);
                    break;
                case "event":
                    $resultStr = $this->MsgTypeEvent($postObj);
                    break;
                default:
                    $resultStr = "";
                    break;
            }
        }
      

        if (!empty($resultStr)) {
            echo $resultStr;
        } else {
            echo '';
        }
    }

// 事件自动回复 MsgType = Event
    private function MsgTypeEvent($postObj)
    {
        $contentStr = "";
        switch ($postObj->Event) {
            case "subscribe": // 关注公众号 添加关注回复
                $content = $this->wchat->getSubscribeReplay($this->instance_id);
                if (!empty($content)) {
                    $contentStr = $content;
                }
                if(!empty($postObj->EventKey)){//  关注事件接收参数
                    $OpenID = $postObj->FromUserName;
                    $EventKey = str_replace('qrscene_','',$postObj->EventKey);
                    $arr=explode('@@@',$EventKey);
                    $promotion['title']=$arr[0];
                    $promotion['code'] =$arr[1];
                    $promotion['add_time']=time();
                    $promotion['qrcode']=$postObj->Ticket;

                    $users =db('users')->where(array('openid'=>$OpenID))->find();
                    if(!$users){

                        $access_token = $this->wchat->get_access_token();                     
                        $url =  'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid='.$OpenID.'&lang=zh_CN';   
                        $res = json_decode(httpRequest($url,'GET'));
                        $nickname=$res->nickname;
                        $headimgurl=$res->headimgurl;
                        $openid=$res->openid;
                        $city=$res->city;
                        $province=$res->province;
                        $country=$res->country;
                        $userid = $model->insert(array('nickname'=>$nickname,'headimgurl'=>$headimgurl,'openid'=>$openid,'oauth'=>'app','reg_time'=>time(),'last_login'=>time(),'city'=> $city,'province'=> $province,'country'=> $country));


                    }else{
                         
                         $userid = $users['user_id'];
                    }   
                        $promotion['user_id']=$userid;
                        $ress = db('promotion_list')->where(array('user_id'=>$users['user_id']))->find();
                        if(!$ress){

                             db('promotion_list')->insert($promotion);
                        }
                       
                       
                    
                }
                // 构造media数据并返回
                break;
            case "unsubscribe": // 取消关注公众号
                break;
            case "VIEW": // VIEW事件 - 点击菜单跳转链接时的事件推送
                // $this->wchat->weichat_menu_hits_view($postObj->EventKey); //菜单计数
                $contentStr = "";
                break;
            case "SCAN": // SCAN事件 - 用户已关注时扫描的事件推送
                $contentStr = "";
                if(!empty($postObj->EventKey)){ // 事件推送
                    $users =db('users')->where(array('openid'=>$OpenID))->find();
                    $res = db('promotion_list')->where(array('user_id'=>$users['user_id']))->find();
                    if(!$res){

                        $EventKey = str_replace('qrscene_','',$postObj->EventKey);
                        $arr=explode('@@@',$EventKey);
                        $promotion['code'] =$arr[0].'@@@扫描';
                        $promotion['title']=$arr[1];
                        $promotion['add_time']=time();
                        $promotion['qrcode']=$postObj->Ticket;
                        $promotion['user_id']=$users['user_id'];
                        db('promotion_list')->insert($promotion);
                    }
                }
                
                break;
            case "CLICK": // CLICK事件 - 自定义菜单事件
                $menu_detail = $this->wchat->getWeixinMenuDetail($postObj->EventKey);
                $media_info = $this->wchat->getWeixinMediaDetail($menu_detail['media_id']);
                $contentStr = $this->wchat->getMediaWchatStruct($media_info); // 构造media数据并返回
                break;
            default:
                break;
        }
        // $contentStr = $postObj->Event."from_callback";//测试接口正式部署之后注释不要删除
        if (is_array($contentStr)) {
            $resultStr = $this->wchat->event_key_news($postObj, $contentStr);
        } else {
            $resultStr = $this->wchat->event_key_text($postObj, $contentStr);
        }
        return $resultStr;
    }

你可能感兴趣的:(PHP,微信公众号)