Thinkphp has just developed a course table editor. I have been trying to develop a function that can query the course table and score. I 've been studying it for some time. so I 'd like to share it with you here. Because there are many small series courses, this article may be very hasty, and the code writing is definitely not very standard. I hope you can give more advice.
Design Concept: first log on to the page to obtain COOKIES, and then take the cookies to the server for verification code. Finally, all the information required by the server is provided. In fact, we simulate the browser access interface. We use cookies instead of manual logon to the educational administration system.
1. the PHP request of CURL is different from the PHP request sent by the browser. Therefore, we use the CURL library to implement our functions.
2. nature of sessionID
The client uses cookies to save the sessionID
The client saves the sessionID with the cookie. when we request the server, the sessionID will be sent to the server together, and the server will search for the corresponding sessionID in the memory. if the corresponding sessionID is found, it indicates that we are logged on and have the corresponding permissions. if the corresponding sessionID is not found, it means that we have disabled the browser (which will be explained later ), if the session times out (no server is requested for more than 20 minutes) and the session is cleared by the server, the server will assign you a new sessionID. You must log on again and save the new sessionID in the cookie.
When the browser is not closed (if the sessionID has been saved in the cookie at this time), the sessionID will be stored in the browser all the time, this sessionID is submitted to the server every time a request is sent, so the server considers that we are logged on. of course, if the server is not requested for a long time, the server will think that we have already disabled the browser. at this time, the server will clear the sessionID from the memory. at this time, if we request the server again, the sessionID will no longer exist, therefore, the server does not find the corresponding sessionID in the memory, so a new sessionID will be generated. in this case, we usually need to log on again.
The client does not use cookies to save sessionID
When the client does not use cookies to save sessionID, if we request the server because sessionID is not submitted, the server will think that you are a brand new request, and the server will assign you a new sessionID, this is why every time we open a new browser (whether we have logged on or not), a new sessionID (or we will log on again) will be generated ).
When we turn off the browser and open the browser and then request the page, it will let us log on. why? We have already logged on and haven't timed out. sessionID must be on the server. why should we log on again now? This is because when we turn off browsing and then request, the information we submitted did not submit the sessionID we just submitted to the server together, so the server does not know that we are the same person, in this case, the server assigns us a new sessionID. for example, the browser is like a person who wants to open an account at a bank, and the server is like a bank, the account owner who wants to open an account in the bank obviously does not have an account (sessionID) at this time. Therefore, after arriving at the bank, the bank staff asked if there was an account and he said no, at this time, the bank will activate an account for him. So it can be said that every time you open a new browser to request a page, the server will think that this is a new request, and it will assign you a new sessionID.
Get_headers () is a PHP System-level function that returns an array containing the header sent by the server to respond to an HTTP request. If it fails, FALSE is returned and an error message at the E_WARNING level is sent (it can be used to determine whether a remote file exists ).
Function definition
Array get_headers (string $ url [, int $ format = 0])
Parameters
Url target URL
If you set the optional format parameter to 1, get_headers () will parse the corresponding information and set the key name of the array.
Not much nonsense about the code:
Class TestController extends Controller {
Public function index (){
$ Url = 'http: // logon address of the academic affairs office ';
$ U = get_headers ($ url, 1 );
$ Login_url = 'http: // logon address of the academic affairs office '. dirname ($ u ['location']).'/default2.aspx ';
$ Hiden = $ this-> getView ($ login_url );
$ Post_fields = array (
'Txtusername' => 'student id ',
'Textbox2 '=> 'password ',
'Radiobuttonlist1' => 'student ',
'_ Viewstate' => $ hiden,
"Button1" => "login"
);
$ Cookie_file = tempnam ('public', 'cooke ');
$ Ch = curl_init ($ login_url );
Curl_setopt ($ ch, CURLOPT_HEADER, 0); // display the header
Curl_setopt ($ ch, CURLOPT_POST, 1); // post transfer
$ Header [] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/1234 ';
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); // simulate the client
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 0); // Display Page
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_fields); // pass the value of post
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_file); // Save the cookie
Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); // redirect allowed
Curl_exec ($ ch );
Curl_close ($ ch );
// Obtain the course schedule
$ Class_url = 'academic affairs logon address '. dirname ($ u ['location']).'/xskbcx. aspx? Xh = '.' student ID ';
$ Main_url = 'logon address of the academic affairs authorization'. dirname ($ u ['location']). '/xs_main.aspx? Xh = '.' student ID ';
$ This-> getClass ($ class_url, $ main_url );
}
// Hidden fields on the login page
Private function getView ($ url ){
$ Result = curl_request ($ url );
$ Pattern = '// is'; preg_match_all ($ pattern, $ result, $ matches );
$ Res [0] = $ matches [1] [0];
Return $ res [0];
}
// Query the course list
Public function getClass ($ url, $ main_url ){
$ Cookie_file = tempnam ('public', 'Cookies ');
$ Ch = curl_init ($ url );
Curl_setopt ($ ch, CURLOPT_TIMEOUT, 60 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_REFERER, $ main_url );
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_file); // Save the cookie
$ Str = curl_exec ($ ch );
$ This-> classResult ($ str );
}
// Return the course table string
Private function classResult ($ result ){
Preg_match_all ('/([\ w \ W] *?) <\\/ Table>/', $ result, $ out );
$ Table = $ out [0] [0];
// Obtain the entire course list
// Print_r ($ table );
Preg_match_all ('/([\ w \ W] *?) <\\/ Td>/', $ table, $ out); $ td = $ out [1]; $ length = count ($ td ); // print_r ($ td );
// Obtain the course list
For ($ I = 0; $ I <$ length; $ I ++ ){
$ Td [$ I] = str_replace ("", "", $ 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); $ this-> converttoTable ($ td );
}
}