通过简单的PHP Curl来对接浙师大后勤学生简单信息!


最近在开发学校一个校级比赛的官网,所以要涉及到学校学生信息资料的获取问题(学校肯定不开放给学生的),所以只能自己写一个能够简单获取学校学生信息的函数来帮助完善这个网站,通过php curl函数来简单的获取对接信息!

对接到后勤网站的好处是可以外网访问。。哈哈哈哈

http___678890.zjnu.edu.cn

这个函数能够获取学生基本信息,比如姓名、性别、学号、住址等等。。我只获取了前三种因为后面这些资料获取了也没用,还加重程序读取效率~还影响安全~

/**
 * @note 获取curl后的contents
 * @param ch
 * @return array array(cookie,body)
 */
function getChCookie(ch){

    // 获取头部信息
    curl_setopt(ch, CURLOPT_HEADER, 1);

    // 返回原生的(Raw)输出
    curl_setopt(ch, CURLOPT_RETURNTRANSFER, true);

    // 执行并获取返回结果
    content = curl_exec(ch);

    // 解析HTTP数据流
    body='';
    if(content!=''){
        list(header,body) = explode("rnrn", content);
        // 解析COOKIE
        preg_match("/set-cookie:([^rn]*)/i",header, matches);
// 后面用CURL提交的时候可以直接使用
// curl_setopt(ch, CURLOPT_COOKIE, cookie);

        if(matches!=null) {
            cookie =matches[1];
        }else{
            cookie='';
        }


    }else{cookie='';
    }
    return array(cookie,body);
}

/**
 * @note 678890.zjnu.edu.cn 支持外网
 * @param account
 * @parampassword
 * @return array array(tipid=0(登录失败,账号或密码错误)|1(系统错误)|2(登录成功))
 */
function login2ZJUN678890(account,password){

    //初始化变量tipid='2';//初始变量,登录成功!
    // cookie_file = ROOT_PATH.'\cookie.txt';cookie_url = 'http://678890.zjnu.edu.cn';
    referer='http://678890.zjnu.edu.cn/home/logintype';login_url = 'http://678890.zjnu.edu.cn/Module/login';
    data_url='http://678890.zjnu.edu.cn/Home/personalinfo';

    // for(i=0;i<ii=2;i++) {curl = curl_init();
    timeout = 10;
    curl_setopt(curl, CURLOPT_URL, cookie_url);
    curl_setopt(curl, CURLOPT_REFERER, referer);
    curl_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);cookie = getChCookie(curl)[0];
    curl_close(curl);

    post = "uname=account&pwd=password";//(unable to decode value)curl = curl_init();
    curl_setopt(curl, CURLOPT_URL,login_url);
    curl_setopt(curl, CURLOPT_REFERER,referer);
    curl_setopt(curl, CURLOPT_HEADER, false);
    curl_setopt(curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt(curl, CURLOPT_POST, 1);
    curl_setopt(curl, CURLOPT_POSTFIELDS, post);
    curl_setopt(curl, CURLOPT_COOKIE, cookie);cookie = getChCookie(curl)[0];contents = getChCookie(curl)[1];
    if(contents=='no'){
        tipid='0';//账号或密码错误
    }curl = curl_init();
    curl_setopt(curl, CURLOPT_URL,data_url);
    curl_setopt(curl, CURLOPT_REFERER,referer);
    curl_setopt(curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
    curl_setopt(curl, CURLOPT_COOKIE, cookie); //获取COOKIE并存储contents = curl_exec(curl);
    curl_close(curl);

    if(tipid!='0') {
        if (strpos(contents, 'Object moved')) {
            tipid = '1';//系统错误,请稍后重试!
        }
    }

    //需要时初始变量id='';name='';sex='';

    id_left = '<td class="lable">学(工)号:</td>
                            <td class="field">';id_right = '</td>
                            <td class="last" rowspan="7"><div class="photo"></div></td>
                        </tr>
                        <tr>
                            <td class="lable">姓名:</td>
                            <td class="field">';

    id_left = strpos(contents, id_left) + strlen(id_left);
    id_right = strpos(contents, id_right);
    if(id_left!=false && id_right!=false) {id = substr(contents,id_left, id_right -id_left);
    }

    name_left = '<td class="lable">姓名:</td>
                            <td class="field">';name_right = '</td>
                        </tr>
                        <tr>
                            <td class="lable">性别:</td>
                            <td class="field">';
    name_left = strpos(contents, name_left) + strlen(name_left);
    name_right = strpos(contents, name_right);
    if(name_left!=false && name_right!=false) {name = substr(contents,name_left, name_right -name_left);
    }

    sex_left = '<td class="lable">性别:</td>
                            <td class="field">';sex_right = '</td>
                        </tr>
                        <tr>
                            <td class="lable">籍贯:</td>
                            <td class="field">';
    sex_left = strpos(contents, sex_left) + strlen(sex_left);
    sex_right = strpos(contents, sex_right);
    if(sex_left!=false && sex_right!=false) {sex = substr(contents,sex_left, sex_right -sex_left);
    }

    return array('tipid'=>tipid,'id'=>id,'name'=>name,'sex'=>sex);

}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注