Skip to content

Commit 0da4467

Browse files
committed
支持最后请求参数记录并调用repeatRequest重放请求;增加set类方法链式请求支持;提供重放demo:demo/48_request_repeat.php
1 parent be893fd commit 0da4467

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

demo/48_request_repeat.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/**
4+
* 简单的模拟登录示例
5+
* @author Zjmainstay
6+
* @website http://www.zjmainstay.cn
7+
* @project https://github.com/Zjmainstay/php-curl
8+
*
9+
* 访问登录页,提交登录表单,查看登录结果
10+
*/
11+
12+
require_once __DIR__.'/../vendor/autoload.php';
13+
14+
$autologin = new PHPCurl\CurlAutoLogin();
15+
16+
$lineBreak = $autologin->getLineBreak();
17+
18+
//获取登录态(失败)
19+
$curl = "curl 'http://demo.zjmainstay.cn/js/simpleAjax/loginResult.php'";
20+
$content = $autologin->execCurl($curl);
21+
echo 'Before Login: ' . $content . $lineBreak;
22+
23+
//登录
24+
$username = 'Zjmainstay';
25+
$curl = "curl 'http://demo.zjmainstay.cn/js/simpleAjax/doPost.php' -H 'Host: demo.zjmainstay.cn' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:47.0) Gecko/20100101 Firefox/47.0' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3' -H 'Accept-Encoding: gzip, deflate' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'X-Requested-With: XMLHttpRequest' -H 'Referer: http://demo.zjmainstay.cn/js/simpleAjax/' -H 'Cookie: Hm_lvt_1526d5aecf5561ef9401f7c7b7842a97=1468327822,1468327904,1468341636,1468411918; Hm_lpvt_1526d5aecf5561ef9401f7c7b7842a97=1468421526' -H 'Connection: keep-alive' --data 'username=[username]'";
26+
$curl = str_replace('[username]', $username, $curl);
27+
$autologin->execCurl($curl, false, false, false); //登录不需要记录最后请求参数
28+
29+
//重放获取登录态
30+
$content = $autologin->repeatRequest();
31+
echo 'After Login: ' . $content . $lineBreak;

src/CurlAutoLogin.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class CurlAutoLogin {
1717
protected $logPath = '';
1818
//全局opt
1919
protected $globalOpts = [];
20+
//最后请求参数
21+
protected $lastExecParams = [];
2022

2123
public function __construct($logPath = '') {
2224
if(!empty($logPath) && is_writable($logPath)) {
@@ -33,16 +35,22 @@ public function __construct($logPath = '') {
3335
public function setGlobalOpts($opts = [])
3436
{
3537
$this->globalOpts += $opts;
38+
return $this;
3639
}
3740

3841
/**
3942
* 根据curl信息执行并解析结果
4043
* @param string $curlContent 利用Firefox浏览器复制cURL命令
4144
* @param boolean $callbackBefore 对curl结果前置处理,如更换用户名、密码等
4245
* @param boolean $callbackAfter 对采集结果后置处理,如解析结果的csrf token等
46+
* @param boolean $storeParams 是否存储最后请求参数供重放使用,默认存储
4347
* @return mixed
4448
*/
45-
public function execCurl($curlContent, $callbackBefore = false, $callbackAfter = false) {
49+
public function execCurl($curlContent, $callbackBefore = false, $callbackAfter = false, $storeParams = true) {
50+
//存储参数供请求重放使用
51+
if($storeParams) {
52+
$this->lastExecParams = func_get_args();
53+
}
4654
$parseCurlResult = $this->parseCurl($curlContent);
4755
if(is_callable($callbackBefore)) {
4856
$parseCurlResult = $callbackBefore($parseCurlResult);
@@ -57,6 +65,39 @@ public function execCurl($curlContent, $callbackBefore = false, $callbackAfter =
5765
return $execCurlResult;
5866
}
5967

68+
/**
69+
* 重放请求
70+
* @return mixed
71+
*/
72+
public function repeatRequest()
73+
{
74+
//最后一次请求参数不为空才重放
75+
if(!empty($this->lastExecParams)) {
76+
list($curlContent, $callbackBefore, $callbackAfter) = array_pad($this->lastExecParams, 3, false);
77+
return $this->execCurl($curlContent, $callbackBefore, $callbackAfter, true);
78+
}
79+
return null;
80+
}
81+
82+
/**
83+
* 获取最后一次请求参数
84+
* @return array
85+
*/
86+
public function getLastExecParams()
87+
{
88+
return $this->lastExecParams;
89+
}
90+
91+
/**
92+
* 主动销毁最后一次请求参数
93+
* @return $this
94+
*/
95+
public function unsetLastExecParams()
96+
{
97+
$this->lastExecParams = [];
98+
return $this;
99+
}
100+
60101
/**
61102
* 携带cookie执行curl命令
62103
* @param string $curlContent 利用Firefox浏览器复制cURL命令
@@ -195,6 +236,7 @@ protected function _execCurl($parseCurlResult) {
195236
*/
196237
public function setLogPath($logPath) {
197238
$this->logPath = $logPath;
239+
return $this;
198240
}
199241

200242
/**
@@ -230,13 +272,15 @@ public function setLastCookieFile($cookieFile) {
230272
if(!$this->lockedLastCookieFile) {
231273
$this->lastCookieFile = $cookieFile;
232274
}
275+
return $this;
233276
}
234277

235278
/**
236279
* 清空上次存储的cookie
237280
*/
238281
public function removeLastCookie() {
239282
file_put_contents($this->getLastCookieFile(), '');
283+
return $this;
240284
}
241285

242286
/**
@@ -245,6 +289,7 @@ public function removeLastCookie() {
245289
*/
246290
public function lockLastCookieFile() {
247291
$this->lockedLastCookieFile = true;
292+
return $this;
248293
}
249294

250295
/**
@@ -253,6 +298,7 @@ public function lockLastCookieFile() {
253298
*/
254299
public function unlockLastCookieFile() {
255300
$this->lockedLastCookieFile = false;
301+
return $this;
256302
}
257303

258304
/**

0 commit comments

Comments
 (0)