某高校校园卡余额接口2

<?php           
    $crad = new card;
    $crad->getinfo();
   
class card{
    private $obj = NULL;
    private $cardNum = "";
    private $pwd = "";
    private $info = null;
    private $inputStartDate = "";
    private $inputEndDate = "";
    private $totalP = 0;
    private $type = "json";
    /*
    *初始化卡号,密码参数
    */
    function __construct(){               
        include_once("snoopy.class.php");
        $this->info = new obj;       
        $this->obj = new Snoopy();   
        if(!empty($_REQUEST)){           
            if(ctype_digit($_REQUEST['cardnum']) && ctype_digit($_REQUEST['pwd'])){
                $this->cardNum = intval($_REQUEST['cardnum']);
                $this->pwd = $_REQUEST['pwd'];
                $this->inputStartDate = $_REQUEST['start'];
                $this->inputEndDate = $_REQUEST['end'];   
                $this->type = $_REQUEST['type'];
            }else{
                $this->info->status = 0;
                $this->info->res = "账号密码格式错误";
                echo json_encode($this->info);exit;   
            }
        }
    }
   
    /*
    *根据密码卡,获取真实提交的密码
    */
    function getpwd($imgsrc = "pan.png"){   
        //数字图片横纵坐标
        $width = array(9,45,81);
        $height = array(35,71,107);
        //数字图片对应的数字,这个值在不同服务器上不同,原因不明
        $old = array(
            "1c08ef360c36e3a85efb2ca279802461" => 9,
            "bfc4992573880df8e374807c443e8401" => 0,
            "eb24abcd7028ac9d03848d35ca4376f2" => 1,
            "486d19dccda9cae742bb376f257c7eb9" => 3,
            "da4c60c456ba8d60001da697ecd65a7d" => 2,
            "34d28f6f4f6638cecf8e01df5f8c1cf9" => 5,
            "40333b61dea66ec76af47efc467d7722" => 6,
            "4ed3428507225abf5dc1f0b0624fbbdc" => 8,
            "c230847e76231e5efad5c558544ceb56" => 4,
            "353519ea4fb442c8d23244e66d3bd4bd" => 7,
        );
       
        //获取原图片
        $source = imagecreatefrompng($imgsrc);
       
        $arr = array();
        //设置新图片画布
        $tagurl = "first.png";
        $w = $h = 25;
        $k = 0;
        for($i = 0; $i < count($height); $i++){
            for($j = 0; $j < count($width); $j++){               
                $tag = imagecreatetruecolor($w,$h);               
                imagecopy($tag,$source,0,0,$width[$j],$height[$i],$w,$h);           
                imagepng($tag,$tagurl);
                $zuobiao = md5(file_get_contents($tagurl));               
                $arr[$old[$zuobiao]] = $k;
                $k++;               
            }
        }       
               
        $tag = imagecreatetruecolor($w,$h);               
        imagecopy($tag,$source,0,0,9,143,$w,$h);           
        imagepng($tag,$tagurl);
        $zuobiao = md5(file_get_contents($tagurl));               
        $arr[$old[$zuobiao]] = $k;
                       
        $realpwd = "";       
        for($i = 0; $i < strlen($this->pwd); $i++){
            $realpwd .= $arr[$this->pwd{$i}];   
        }
        return $realpwd;
    }
   
    /*
    *登陆
    */
    function login(){   
        $panurl = "http://card.jnu.edu.cn:8080/getpasswdPhoto.action";       
        $this->obj->fetch($panurl);
        file_put_contents("pan.png",$this->obj->results);
        $this->obj->setcookies();       
       
        $pwd = $this->getpwd("pan.png");
       
        $imgurl = "http://card.jnu.edu.cn:8080/getCheckpic.action?rand=8080.";
        $this->obj->fetch($imgurl);
        $this->obj->setcookies();
       
        $url = "http://card.jnu.edu.cn:8080/loginstudent.action";
        $post = array('name'=>$this->cardNum,'loginType'=>'1','passwd'=>$pwd,'rand'=>'8080','userType'=>'1');   
        $this->obj->submit($url,$post);
        $this->obj->setcookies();                   
    }
   
    /*
    *从表单中获取提交的地址
    */
    function getFormUrl($content){       
        $pattern = "/<form.*?>/i";
        preg_match($pattern,$content,$arr);
       
        $pattern = '/action=".*?"/';
        preg_match($pattern,$arr[0],$url);       
       
        $url = substr($url[0],8);
        $url = substr($url,0,-1);
        return "http://card.jnu.edu.cn:8080".$url;   
    }
   
    /*
    *获取校园卡信息
    */
    function getinfo(){
        $this->login();               
       
        $url = "http://card.jnu.edu.cn:8080/accounthisTrjn.action";
        $this->obj->fetch($url);
        $this->obj->setcookies();
        $res = $this->obj->results;               
               
        $url = $this->getFormUrl($res);
        $post = array('account'=>$this->cardNum,'inputObject'=>'all','Submit'=>'+%C8%B7+%B6%A8+');
        $this->obj->submit($url,$post);
        $this->obj->setcookies();
        $res = $this->obj->results;   
       
        $url = $this->getFormUrl($res);               
        $post = array('inputStartDate'=>$this->inputStartDate,'inputEndDate'=>$this->inputEndDate);
        $this->obj->submit($url,$post);
        $this->obj->setcookies();
        $this->obj->maxredirs = 5;
        $res = $this->obj->results;       
                                       
        $pattern = "/<form.*?>/i";
        preg_match($pattern,$res,$arr);       
        $pattern = '/action=".*?"/';
        preg_match($pattern,$arr[0],$url);               
        $url = substr($url[0],8);
        $url = substr($url,0,-1);
        $url = "http://card.jnu.edu.cn:8080/accounthisTrjn.action".$url;               
        $this->obj->maxredirs = 5;       
        $this->obj->fetch($url);
        $this->obj->setcookies();       
       
       
        $this->getJsonInfo($this->obj->results);
    }   
   
    /*
    *获取具体消息
    */   
    public function getJsonInfo($res){   
        $this->getFirstPage();
        //如果没有第一页数据,请求失败
        if(empty($this->info->res)){
            $this->info->status = 0;
            echo json_encode($this->info);exit(0);
        }else{
            $this->info->status = 1;
        }
       
        //获取从第二页起到最后一页的数据       
        for($i = 2; $i<=$this->totalP; $i++){
            $url = "http://card.jnu.edu.cn:8080/accountconsubBrows.action";
            $post = array('pageNum'=>$i,'inputStartDate'=>$this->inputStartDate,'inputEndDate'=>$this->inputEndDate);
            $this->obj->submit($url,$post);
           
            //获取数据
            $pattern = "/<tr\sclass=\"listbg2?\">.*?<\/tr>/is";
            preg_match_all($pattern,$this->obj->results,$arr);
            $pattern = "/<td.*?>(.*?)<\/td>/si";
            $res = array();       
            foreach($arr[0] as $key => $value){
                preg_match_all($pattern,$value,$rr);
                $res[] = $rr[1];   
            }                           
            $this->info->res = array_merge($this->info->res,$this->gbkToUtf8($res));                               
        }
        //给数据设置新键名
        $this->setKey($this->info->res);   
       
        if($this->type == "json"){
            header("Content-Type:application/json;charset=utf8");
            echo json_encode($this->info);               
            //var_dump($this->info->res);
        }
    }
   
    /*
    *获取第一页数据,并且获取总页数
    */
    private function getFirstPage(){
        //第一次获取数据
        $url = "http://card.jnu.edu.cn:8080/accountconsubBrows.action";
        $post = array('pageNum'=>'1','inputStartDate'=>$this->inputStartDate,'inputEndDate'=>$this->inputEndDate);
        $this->obj->host = "card.jnu.edu.cn:8080";       
        $this->obj->agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0";
        $this->obj->referer = "http://card.jnu.edu.cn:8080/accountconsubBrows.action";
        $this->obj->submit($url,$post);
        $this->obj->setcookies();
       
        //获取一共几页
        $pattern = "/<tr\sclass=\"bl\">.*?<\/tr>/is";                   
        preg_match($pattern,$this->obj->results,$arr1);
        $str = $this->gbkToUtf8($arr1[0]);   
        $pattern = "/(?<=共).*?(?=页)/is";
        preg_match($pattern,$str,$rr);
        $pattern = "/\d*$/";
        preg_match($pattern,$rr[0],$ar);
        $this->totalP = $ar[0];   
       
        //获取数据
        $pattern = "/<tr\sclass=\"listbg2?\">.*?<\/tr>/is";
        preg_match_all($pattern,$this->obj->results,$arr);
        $pattern = "/<td.*?>(.*?)<\/td>/si";
        $res = array();       
        foreach($arr[0] as $key => $value){
            preg_match_all($pattern,$value,$rr);
            $res[] = $rr[1];   
        }   
       
        $this->info->res = $this->gbkToUtf8($res);           
    }
   
    /*
    *将gb2312编码的中文转为utf8
    */
    private function gbkToUtf8($arr){
        if(is_array($arr)){
            foreach($arr as $key => $value){               
                $arr[$key] = $this->gbkToUtf8($value);                   
            }
        }else{
            return iconv("GB2312","UTF-8//IGNORE",$arr);      
        }
        return $arr;
    }       
   
    /*
    *给二维数组设置新键名
    */
    private function setKey(&$ar){
        //键名数组
        $kname = array('time', 'way', 'where', 'pay', 'rest','count','status');       
        array_walk($ar, array($this,'foo'), $kname);
    }
   
    /*
    *给一维数组设置新键名
    */
    private function foo(&$v, $k, $kname){
        $v = array_combine($kname, array_slice($v, 0));   
    }
}

/*
*json数据类
*/
class obj{
    public $status = 0;   
    public $res = array();
}
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值