Home Backend Development PHP Tutorial thinkphp微信开发获取课表

thinkphp微信开发获取课表

Jun 23, 2016 pm 01:10 PM

小编刚刚接触微信开发,一直想做一个自己能查询课表和成绩的微信功能,在研究了些许时间,故在这里和大家一起分享一下。因为平常小编课程比较多,这篇文章可能写的很仓促,代码写的肯定不是很规范,还希望大家多交流指点。


设计思路:先登录页面获取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 );

}

}

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

See all articles