Skip to content

Commit bdb50cc

Browse files
committed
增加cookie手动追加功能;兼容curl命令中 -d/--data ;file_put_contents 函数调用前判断文件存在;
1 parent 0da4467 commit bdb50cc

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

src/CurlAutoLogin.php

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,14 @@ public function parseCurl($curlContent) {
143143
}
144144

145145
//get data
146-
if(!preg_match("#--data \\$?'([^']*)'#is", $curlContent, $postDataMatch)) {
147-
$postData = '';
148-
} else {
149-
$postData = $postDataMatch[1];
146+
//单引号
147+
if(!preg_match("#(?:--data\S*|-d) \\$?'([^']*)'#is", $curlContent, $postDataMatch)) {
148+
//双引号
149+
if(!preg_match('#(?:--data\S*|-d) \\$?"([^"]*)"#is', $curlContent, $postDataMatch)) {
150+
$postDataMatch[1] = '';
151+
}
150152
}
153+
$postData = $postDataMatch[1];
151154

152155
return array(
153156
'url' => $matchUrl[1],
@@ -245,7 +248,11 @@ public function setLogPath($logPath) {
245248
* @return [type] [description]
246249
*/
247250
protected function _log($msg) {
248-
file_put_contents($this->logPath, $msg . "\n", FILE_APPEND);
251+
try {
252+
$res = file_put_contents($this->logPath, $msg . "\n", FILE_APPEND);
253+
} catch (\Exception $e) {
254+
error_log("CurlAutoLogin 无法写入日志文件 {$this->logPath}: {$msg}");
255+
}
249256
}
250257

251258
/**
@@ -261,7 +268,25 @@ public function getLastCookieFile() {
261268
* @return string
262269
*/
263270
public function getLastCookieContent() {
264-
return file_get_contents($this->getLastCookieFile());
271+
if($file = $this->getLastCookieFile()) {
272+
if(file_exists($file)) {
273+
return file_get_contents($file);
274+
}
275+
}
276+
return '';
277+
}
278+
279+
/**
280+
* 手动追加cookie内容
281+
* @param $content
282+
* @return bool|int
283+
*/
284+
public function appendCookieContent($content)
285+
{
286+
if(file_exists($file = $this->getLastCookieFile())) {
287+
return file_put_contents($file, $content . "\n", FILE_APPEND);
288+
}
289+
return false;
265290
}
266291

267292
/**
@@ -279,7 +304,12 @@ public function setLastCookieFile($cookieFile) {
279304
* 清空上次存储的cookie
280305
*/
281306
public function removeLastCookie() {
282-
file_put_contents($this->getLastCookieFile(), '');
307+
if($file = $this->getLastCookieFile()) {
308+
//文件存在才清空
309+
if(file_exists($file)) {
310+
file_put_contents($file, '');
311+
}
312+
}
283313
return $this;
284314
}
285315

0 commit comments

Comments
 (0)