PHP Realization Simulation Landing founder educational system grasping timetable _php skill

Source: Internet
Author: User
Tags curl

The two applications of course lattice and super timetable, presumably college students are very familiar with the use of their own school number and the Dean's system password, you can import their own timetable, anytime and anywhere can be viewed on the phone.

In fact, a little bit of knowledge of PHP, we can do a similar web application.

1, solve the verification code

In fact, this is a small bug, when we enter the login interface, the browser will go to request the server, the server will generate a CAPTCHA image. If we do not request this picture, then the square backstage will not generate the corresponding verification code, so we have an opportunity, let me happy for a while ~ at this time, we do not fill in the case of verification code, you can very smooth entry. You can prohibit access to the address of the verification code on your computer, and then try this is not true ~ Of course, this is only valid for the affirmative.

2,php's Curl Analog landing

Next is the relevant code, I believe a lot of people like me, just love to see examples, for a lengthy explanation, turn around ... But this habit is still not good ... Don't say much nonsense!

Simulate landing function curl_request ($url, $post = ', $cookie = ', $returnCookie =0) {$curl = Curl_init ();
      curl_setopt ($curl, Curlopt_url, $url); curl_setopt ($curl, Curlopt_useragent, ' mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1;
      trident/6.0) ');
      curl_setopt ($curl, curlopt_followlocation, 1);
      curl_setopt ($curl, Curlopt_autoreferer, 1); curl_setopt ($curl, Curlopt_referer, "here must be replaced by the academic system login URL");
        Fill in the Teaching system URL if ($post) {curl_setopt ($curl, Curlopt_post, 1);
      curl_setopt ($curl, Curlopt_postfields, Http_build_query ($post));
      } if ($cookie) {curl_setopt ($curl, Curlopt_cookie, $cookie);
      curl_setopt ($curl, Curlopt_header, $returnCookie);
      curl_setopt ($curl, Curlopt_timeout, 20);
      curl_setopt ($curl, Curlopt_returntransfer, 1);
      $data = curl_exec ($curl);
      if (Curl_errno ($curl)) {return curl_error ($curl);
      } curl_close ($curl); if ($returnCookie) {list ($header, $Body) = explode ("\r\n\r\n", $data, 2); Preg_match_all ("/set\-cookie: [^;]
        *);/", $header, $matches);
        $info [' cookie '] = substr ($matches [1][0], 1);
        $info [' content '] = $body;
      return $info;
      }else{return $data;
 }
  }

3, the Academic System landing page hidden fields

Give me a chestnut.

Copy Code code as follows:

<input type= "hidden" name= "__viewstate" value= "ddwyode2ntm0otg7oz61eibnkvojbiogytg2vsy2sklwia==" >

These things in the landing time is also need to take, by the way posted function, incidentally, the school of the blogger ... Royal College of Farming (mainly the use of regular expressions)

The hidden field on the landing page

function GetView () {
  $url = ' http://jw.hzau.edu.cn/default2.aspx ';
  $result = Curl_request ($url);
  $pattern = '/<input type= ' hidden ' name= ' __viewstate ' value= ' (. *?) "\/>/is";
  Preg_match_all ($pattern, $result, $matches);
  $res [0] = $matches [1][0];
     
  return $res [0];
}
Returns the hidden value of a classroom query page
private function Getviewjs ($cookie, $xh) {
$url = "http://jw.hzau.edu.cn/xxjsjy.aspx?xh={$    XH} ";
$result = Curl_request ($url, ', $cookie);
$pattern = '/<input type= ' hidden ' name= ' __viewstate ' value= ' (. *?) "\/>/is";
Preg_match_all ($pattern, $result, $matches);
$res [0] = $matches [1][0];
return $res [0];
}

Acquisition of 4,cookie

function login ($xh, $pwd) {
    $url = ' http://jw.hzau.edu.cn/default2.aspx ';
    $post [' __viewstate '] = $this->getview ();
    $post [' txtusername '] = $xh; Fill in the number
    $post [' TextBox2 '] = $pwd;//fill in the password
    $post [' txtsecretcode '] = ';
    $post [' lblanguage '] = ';
    $post [' Hidpdrs '] = ';
    $post [' hidsc '] = ';
    $post [' RadioButtonList1 '] = iconv (' utf-8 ', ' gb2312 ', ' student ');
    $post [' Button1 '] = iconv (' utf-8 ', ' gb2312 ', ' login ');
    $result = Curl_request ($url, $post, ' ', 1);
    return $result [' Cookie '];
  }

5, let's try to check the function of the timetable, the format is a bit messy, we make it look, I turned the timetable into a two-dimensional associative array

Returns the schedule string private function Classresult ($xh, $pwd) {date_default_timezone_set ("PRC");//time zone Set $classList = "";//Declaration Timetable
    Variable $cookie = $this->login ($xh, $pwd); $view = $this->getviewjs ($cookie, $XH);//Verify password is correct//if the password is correct if (!empty ($view)) {$url = "http://jw.hzau.e
      du.cn/xskbcx.aspx?xh={$xh} "; $result = Curl_request ($url, ', $cookie); Saved Cookies Preg_match_all ('/<table id= ' Table1 ' [\w\w]*?> ([\w\w]*?)
      <\/table>/', $result, $out); $table = $out [0][0]; Get the entire schedule Preg_match_all ('/<td [\w\w]*?> (\w\w]*?)
      <\/td>/', $table, $out);
      $TD = $out [1];
 
      $length = count ($TD);
 
        Get the course list for ($i =0; $i < $length; $i + +) {$TD [$i] = Str_replace ("<br>", "", $td [$i]);
       
        $reg = "/{(. *)}/";
        if (!preg_match_all ($reg, $TD [$i], $matches)) {unset ($td [$i]); }} $TD = Array_values ($TD);
      Re-index the course list array $tdLength = count ($TD); for ($i =0; $i < $tdLength;
      $i + +) {$TD [$i] = iconv (' GB2312 ', ' UTF-8 ', $td [$i]);
            ///Convert the timetable to a set of form function ConvertToTable ($table) {$list = array (' Sun ' => Array (  ' 1,2 ' => ', ' 3,4 ' => ', ' 5,6 ' => ', ' 7,8 ' => ', ' 9,10 ' => '), ' Mon ' => array (' 1,2 ' => ', ' 3,4 ' => ', ', ' 5
            , 6 ' => ', ' 7,8 ' => ', ' 9,10 ' => '), ' Tues ' => Array ( ' 1,2 ' => ', ' 3,4 ' => ', ' 5,6 ' => ', ' 7,8 ' => ', ' 9,10 ' => '), ' Wed ' => array (' 1,2 ' => ', ' 3,4 ' => ', ', ' 5,
            6 ' => ', ' 7,8 ' => ', ' 9,10 ' => '), ' Thur ' => Array ( ' 1,2 ' => ', ' 3,4 ' => ', ' 5,6 '=> ', ' 7,8 ' => ', ' 9,10 ' => '), ' Fri ' => Array (' 1  , 2 ' => ', ' 3,4 ' => ', ' 5,6 ' => ', ' 7,8 ' => ', ' 9,10 ' => '), ' sat ' => array (' 1,2 ' => ', ' 3,4 ' => ', ' 5,6 ' =& Gt
        ', ' 7,8 ' => ', ' 9,10 ' => '); $week = Array ("Sun" => "Sunday", "Mon" => "Monday", "Tues" => "Tuesday", "Wed" => "Wednesday", "Thur" => "Thursday", "Fri" => "Friday", "sat" =
        > "Saturday");
        $order = Array (' 1,2 ', ' 3,4 ', ' 5,6 ', ' 7,8 ', ' 9,10 ');
          foreach ($table as $key => $value) {$class = $value;
            foreach ($week as $key => $weekDay) {$pos = Strpos ($class, $weekDay);
            Echo $pos; if ($pos) {$weekArrayDay = $key;//Get the first dimension key foreach ($order as $key => in the list array $orderClass ) {$pos = strPOS ($class, $orderClass);
                if ($pos) {$weekArrayOrder = $orderClass;//Get the course is the section break;
            }} break;
        }} $list [$weekArrayDay] [$weekArrayOrder] = $class;
      return $list;
    //Call function return ConvertToTable ($TD);
    }else{return 0;
 }
  }

6, and then try to query the function of the empty classroom

Empty classroom Query Results Public Function Roomresult () {$xh = ""; Set the school number $pwd = "";
    Number of the corresponding password $cookie = $this->login ($xh, $pwd);
    $url = "http://jw.hzau.edu.cn/xs_main.aspx?xh={$xh}"; $result = Curl_request ($url, ', $cookie);
    stored cookies $url = "http://jw.hzau.edu.cn/xxjsjy.aspx?xh={$xh}";
    $post [' Button2 '] = iconv (' utf-8 ', ' gb2312 ', ' empty classroom query ');
    $post [' __eventargument ']= ';
    $post [' __eventtarget ']= ';
    $post [' __viewstate '] = $this->getviewjs ($cookie, $XH);
    $post [' ddldsz '] = iconv (' utf-8 ', ' gb2312 ', ' single '); $post [' ddlsyxn '] = ' 2014-2015 ';
    School year $post [' ddlsyxq '] = ' 1 ';
    $post [' jslb '] = ';
 
    $post [' xiaoq '] = '; $post [' KSSJ ']=$_get[' start '];
    The start time submitted $post [SJD ']=$_get[' class '];//the course section submitted $post [' xn ']= ' 2014-2015 ';//the school year $post [' Xq ']= ' 2 '; $post [' Xqj ']= ' 6]//day of the week $post [' dpdatagrid1:txtpagesize ']=90;//each page shows the number of bars $result = Curl_request ($url, $post, $cookie
     
    , 0); Preg_match_all ('/<span[^>]+>[^>]+span>/', $result, $out); $tip = Iconv (' gb2312 ', ' utf-8 ', $out [0][3]);//Get the prompt content of the front page preg_match_all ('/<table[\w\w]*?>?
    <\/table>/', $result, $out); $table = Iconv (' gb2312 ', ' utf-8 ', $out [0][0]);
  Gets the query list $this->load->view ("classroom", Array (' Tip ' => $tip, ' table ' => $table));
 }

Summed up is these, each school's academic system is not the same, at this time we can use Firefox browser Firebug grab bag, to see exactly what is submitted.

The above mentioned is the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.