Skip to content

Commit

Permalink
增加cookie手动追加功能;兼容curl命令中 -d/--data ;file_put_contents 函数调用前判断文件存在;
Browse files Browse the repository at this point in the history
  • Loading branch information
Zjmainstay committed Jan 17, 2019
1 parent 0da4467 commit bdb50cc
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/CurlAutoLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,14 @@ public function parseCurl($curlContent) {
}

//get data
if(!preg_match("#--data \\$?'([^']*)'#is", $curlContent, $postDataMatch)) {
$postData = '';
} else {
$postData = $postDataMatch[1];
//单引号
if(!preg_match("#(?:--data\S*|-d) \\$?'([^']*)'#is", $curlContent, $postDataMatch)) {
//双引号
if(!preg_match('#(?:--data\S*|-d) \\$?"([^"]*)"#is', $curlContent, $postDataMatch)) {
$postDataMatch[1] = '';
}
}
$postData = $postDataMatch[1];

return array(
'url' => $matchUrl[1],
Expand Down Expand Up @@ -245,7 +248,11 @@ public function setLogPath($logPath) {
* @return [type] [description]
*/
protected function _log($msg) {
file_put_contents($this->logPath, $msg . "\n", FILE_APPEND);
try {
$res = file_put_contents($this->logPath, $msg . "\n", FILE_APPEND);
} catch (\Exception $e) {
error_log("CurlAutoLogin 无法写入日志文件 {$this->logPath}: {$msg}");
}
}

/**
Expand All @@ -261,7 +268,25 @@ public function getLastCookieFile() {
* @return string
*/
public function getLastCookieContent() {
return file_get_contents($this->getLastCookieFile());
if($file = $this->getLastCookieFile()) {
if(file_exists($file)) {
return file_get_contents($file);
}
}
return '';
}

/**
* 手动追加cookie内容
* @param $content
* @return bool|int
*/
public function appendCookieContent($content)
{
if(file_exists($file = $this->getLastCookieFile())) {
return file_put_contents($file, $content . "\n", FILE_APPEND);
}
return false;
}

/**
Expand All @@ -279,7 +304,12 @@ public function setLastCookieFile($cookieFile) {
* 清空上次存储的cookie
*/
public function removeLastCookie() {
file_put_contents($this->getLastCookieFile(), '');
if($file = $this->getLastCookieFile()) {
//文件存在才清空
if(file_exists($file)) {
file_put_contents($file, '');
}
}
return $this;
}

Expand Down

0 comments on commit bdb50cc

Please sign in to comment.