小编刚刚接触微信开发,一直想做一个自己能查询课表和成绩的微信功能,在研究了些许时间,故在这里和大家一起分享一下。因为平常小编课程比较多,这篇文章可能写的很仓促,代码写的肯定不是很规范,还希望大家多交流指点。
设计思路:先登录页面获取COOKIES,然后拿着cookies找服务器要验证码。最后提供服务器需要的全部信息。其实通俗的讲就是我们模拟浏览器访问界面,我们用cookies去代替人工去登录教务系统。
1、CURL的PHP请求跟你游览器发出的PHP属于两个不同的线程,所以,我们采用CURL这个库来进行我们功能的实现。
2、sessionID的本质
客户端用cookie保存了sessionID
客户端用cookie保存了sessionID,当我们请求服务器的时候,会把这个sessionID一起发给服务器,服务器会到内存中搜索对应的sessionID,如果找到了对应的 sessionID,说明我们处于登录状态,有相应的权限;如果没有找到对应的sessionID,这说明:要么是我们把浏览器关掉了(后面会说明为什 么),要么session超时了(没有请求服务器超过20分钟),session被服务器清除了,则服务器会给你分配一个新的sessionID。你得重 新登录并把这个新的sessionID保存在cookie中。
在没有把浏览器关掉的时候(这个时候假如已经把sessionID保存在cookie中了)这个sessionID会一直保存在浏览器中,每次请求的时候都会把这个sessionID提交到服务器,所以服务器认为我们是登录的;当然,如果太长时间没有请求服务器,服务器会认为我们已经所以把浏览器关掉了,这个时候服务器会把该sessionID从内存中清除掉,这个时候如果我们再去请求服务器,sessionID已经不存在了,所以服务器并没有在内存中找到对应的 sessionID,所以会再产生一个新的sessionID,这个时候一般我们又要再登录一次。
客户端没有用cookie保存sessionID
客户端没有用cookie保存sessionID这个时候如果我们请求服务器,因为没有提交sessionID上来,服务器会认为你是一个全新的请求,服务器会给你分配一个新的sessionID,这就是为什么我们每次打开一个新的浏览器的时候(无论之前我们有没有登录过)都会产生一个新的sessionID(或者是会让我们重新登录)。
当我们一旦把浏览器关掉后,再打开浏览器再请求该页面,它会让我们登录,这是为什么?我们明明已经登录了,而且还没有超时,sessionID肯定还在服 务器上的,为什么现在我们又要再一次登录呢?这是因为我们关掉浏览再请求的时候,我们提交的信息没有把刚才的sessionID一起提交到服务器,所以服务器不知道我们是同一个人,所以这时服务器又为我们分配一个新的sessionID,打个比方:浏览器就好像一个要去银行开户的人,而服务器就好比银行, 这个要去银行开户的人这个时候显然没有帐号(sessionID),所以到银行后,银行工作人员问有没有帐号,他说没有,这个时候银行就会为他开通一个帐号。所以可以这么说,每次打开一个新的浏览器去请求的一个页面的时候,服务器都会认为,这是一个新的请求,他为你分配一个新的sessionID。
get_headers() 是PHP系统级函数,他返回一个包含有服务器响应一个 HTTP 请求所发送的标头的数组。如果失败则返回 FALSE 并发出一条 E_WARNING 级别的错误信息(可用来判断远程文件是否存在)。
函数定义
array get_headers ( string $url [, int $format = 0 ] )
参数
url 目标 URL
format 如果将可选的 format 参数设为 1,则 get_headers() 会解析相应的信息并设定数组的键名。
废话不多说上代码:
class TestController extends Controller {
public function index() {
$url = 'http://教务处登录地址';
$u = get_headers ( $url, 1 );
$login_url = 'http://教务处登录地址' . dirname ( $u ['Location'] ) . '/default2.aspx';
$hiden = $this->getView ( $login_url );
$post_fields = array (
'txtUserName' => '学号',
'TextBox2' => '密码',
'RadioButtonList1' => '学生',
'__VIEWSTATE' => $hiden,
"Button1" => " 登录 "
);
$cookie_file = tempnam ( 'Public', 'cookie' );
$ch = curl_init ( $login_url );
curl_setopt ( $ch, CURLOPT_HEADER, 0 ); // 显示头部
curl_setopt ( $ch, CURLOPT_POST, 1 ); // post传递
$header [] = 'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36';
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header ); // 模拟客户端
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 0 ); // 显示页面
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_fields ); // post传递值
curl_setopt ( $ch, CURLOPT_COOKIEJAR, $cookie_file ); // 存入cookie
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 ); // 允许重定向
curl_exec ( $ch );
curl_close ( $ch );
// 获取课表
$class_url = '教务处登录地址' . dirname ( $u ['Location'] ) . '/xskbcx.aspx?xh=' . '学号';
$main_url = '教务处登录地址' . dirname ( $u ['Location'] ) . '/xs_main.aspx?xh=' . '学号';
$this->getClass ( $class_url, $main_url );
}
// 登陆页面的隐藏字段
private function getView($url) {
$result = curl_request ( $url );
$pattern = '//is';preg_match_all ( $pattern, $result, $matches );
$res [0] = $matches [1] [0];
return $res [0];
}
// 查询课表
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 ); // 存入cookie
$str = curl_exec ( $ch );
$this->classResult ( $str );
}
// 返回课表字符串
private function classResult($result) {
preg_match_all ( '/([\\w\\W]*?)/', $result, $out );
$table = $out [0] [0];
// 获取整个课表
//print_r($table);
preg_match_all ( '/([\\w\\W]*?)/', $table, $out );$td = $out [1];$length = count ( $td ); //print_r($td) ;
// 获得课程列表
for($i = 0; $i
$td [$i] = str_replace ( "", "", $td [$i] );
$reg = "/{(.*)}/";
if (! preg_match_all ( $reg, $td [$i], $matches )) {
unset ( $td [$i] );}
}
$td = array_values($td); //将课程列表数组重新索引$tdLength = count($td);$this->converttoTable ( $td );
}
}

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Notepad++7.3.1
Easy-to-use and free code editor