微信公众开发平台接收各种数据

<?php
/**
 * 后台内部账户控制器
 * @copyright www.ylwkj.com
 * @author sunbingchao
 * @email sbc@ylwkj.com
 * @date 2012-12-27
 */
class WeixinAction extends AdminAction {


    //微信、易信验证token
    public $token='ylwkj';
    //微信、易信提交过来的内容
    public $postObj;
    //公众微信ID号
    public $myid;
    //用户ID号
    public $userid;
    //创建时间
    public $time;
   

    //微信提供的APPID
    public $APPID;
    public $APPSECRET;    //微信提供的APPSECRET
    public $MsgType;
    public $textTpl;
    public $Access_Token;

   

   
        //通过页面上的网址路径将图片保存在本地
        function getImg($url = "", $filename = ""){

         //去除URL连接上面可能的引号
           //$url = preg_replace( '/(?:^['"]+|['"/]+$)/', '', $url );
          $hander = curl_init();
          $fp = fopen($filename,'wb');
          curl_setopt($hander,CURLOPT_URL,$url);
          curl_setopt($hander,CURLOPT_FILE,$fp);
          curl_setopt($hander,CURLOPT_HEADER,0);
          curl_setopt($hander,CURLOPT_FOLLOWLOCATION,1);
          //curl_setopt($hander,CURLOPT_RETURNTRANSFER,false);//以数据流的方式返回数据,当为false是直接显示出来
          curl_setopt($hander,CURLOPT_TIMEOUT,60);
          curl_exec($hander);
          curl_close($hander);
          fclose($fp);
          Return true;
        }

        //获取access_token数据

        public function getAA(){
            $AppId = "wx9de5e536db6f953f";
            $AppSecret = "6efdcf4e0db4dc3ef4749b79f4e1c893";
            $url_access = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$AppId."&secret=".$AppSecret;
            
            $data = $this->getgroup($url_access);
            $data = substr($data,0,-1);//获取到的access_token数据

            return $data['access_token'];
        }

        //通过url获取jsno数据
        function getgroup($url){  

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
                $json_str = curl_exec($ch);
                return $json_str;
        }

      


         function index(){
            // $url="http://www.56.com/u36/v_NDUzNjM2NzM.html";
            // $save_url="./public/weixin/".time().'.WAV';
            // $this->getImg($url,$save_url);


           // $this->getImg($url,'c:/weixin/'.time().'.jpg');
           

                $this->valid();

                
                if($this->MsgType=="text"){
                    //$this->addlog($this->getMoralInfo($this->keyword));
                    $this->sendMessage($this->getMoralInfo($this->keyword));
                }else if($this->MsgType=="location"){
                    
                }
            }


    function _initialize(){
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
           
            $data['ToUserName'] = $this->myid = $postObj->ToUserName;//开发者微信号
            $data['FromUserName'] = $this->userid = $postObj->FromUserName;//发送方账号
            $data['CreateTime'] = $this->time = $postObj->CreateTime;//消息创建时间
            $data['MsgID'] = $this->msgid = $postObj->MsgID;//消息ID

            $data['MsgType'] = $this->MsgType=$postObj->MsgType;//发送文件类型
            
            $this->Access_Token = $this->getAA();//获取access_token码

            

            if($this->MsgType=="text"){
               // $this->keyword = trim($postObj->Content);
                $data['Content'] = $this->Content = $postObj->Content;//获取文本内容
                $result = $this->addMessage('weixintext',$data);

            }elseif($this->MsgType=="location"){
                $data['Location_X'] = $this->Location_X=$postObj->Location_X;
                $data['Location_Y'] = $this->Location_Y=$postObj->Location_Y;
                $data['Scale'] = $this->scale = $postObj->Scale;
                $data['Label'] = $this->label = $postObj->Label;
                $result = $this->addMessage('weixinlocation',$data);

            }elseif($this->MsgType == 'image'){
                $data['PicUrl'] = $this->PicUrl = $postObj->PicUrl;
                $data['MediaId'] = $this->MediaId = $postObj->MediaId;
                $result = $this->addMessage('weixinimage',$data);

            $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$this->Access_Token."&media_id=".$data['MediaId'];//获取图片的信息
            $urlOne =$this->getImg($url,'filename.png');
            $url = json_decode($urlOne);
            $data['path'] = $url[''];

            }else if($this->MsgType =='voice'){
                $data['MediaId'] = $this->MediaId = $postObj->MediaId;
                $data['Format'] = $this->format = $postObj->Format;
                $result = $this->addMessage('weixinvoice',$data);
            
            $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$this->Access_Token."&media_id=".$data['MediaId'];//获取声音的信息
            $urlOne =$this->getImg($url,'filename.WAV');
            $url = json_decode($urlOne);
            $data['path'] = $url[''];

            }else if($this->MsgType=='video'){
                $data['MediaId'] = $this->MediaId = $postObj->MediaId;
                $data['ThumbMediaId'] = $this->thumbMediaId = $postObj->ThumbMediaId;
                $result = $this->addMessage('weixinvideo',$data);
            
            $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$this->Access_Token."&media_id=".$data['MediaId'];//获取视频的信息
            $urlOne =$this->getImg($url,'filename.WAV');
            $url = json_decode($urlOne);
            $data['path'] = $url[''];

            }else if($this->MsgType=='link'){
                $data['Title'] = $this->title = $postObj->Title;
                $data['Description'] = $this->Description = $postObj->Description;
                $data['Url'] = $this->url =$postObj->Url;
                $result = $this->addMessage('weixinlink',$data);

            }
        }
    }
    //添加文本信息的表
    public function addMessage($table,$data){
        $result = M($table)->add($data);
        return $result;
    }
    /**
     * @param 回复消息的内容 $content
     *
     */
    function sendMessage($content){
        $this->textTpl = "<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <Content><![CDATA[%s]]></Content>
                <FuncFlag>0</FuncFlag>
                </xml>";
        $this->msgType = "text";
        $resultStr = sprintf($this->textTpl, $this->userid, $this->myid, time(), $this->msgType, $content);
        echo $resultStr;
        exit();
    }
    /**
     * @param unknown $arr
     * $arr包括title标题,intro简介,imagepath图片路径,url访问路径,四个属性
     * 图片路径用绝对路径,即http://开头
     */
    function sendNews($arr){
        $this->msgType = "news";
        $this->textTpl="<xml>
                <ToUserName><![CDATA[".$this->userid."]]></ToUserName>
                        <FromUserName><![CDATA[".$this->myid."]]></FromUserName>
                                <CreateTime>".time()."</CreateTime>
                                        <MsgType><![CDATA[".$this->msgType."]]></MsgType>
                                                <ArticleCount>".count($arr)."</ArticleCount>
                                                        <Articles>";
        foreach ($arr as $key =>$list){
            $this->textTpl.="<item>
                    <Title><![CDATA[".$list['title']."]]></Title>
                            <Description><![CDATA[".$list['name']."]]></Description>
                                    <PicUrl><![CDATA[".$list['imagapath']."]]></PicUrl>
                                    <Url><![CDATA[".$list['url']."]]></Url>
                                    </item>
                                    ";
        }
        $this->textTpl.="</Articles></xml>";
        echo $this->textTpl;
        exit();
    }
    /**
     *验证token方法
     */
    public function valid(){
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }
    function checkSignature(){
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $tmpArr = array($this->token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值