From bdb50cca3efa2044d0a1aae37d57dffab152a6ed Mon Sep 17 00:00:00 2001 From: Zjmainstay Date: Thu, 17 Jan 2019 17:10:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0cookie=E6=89=8B=E5=8A=A8?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0=E5=8A=9F=E8=83=BD=EF=BC=9B=E5=85=BC=E5=AE=B9?= =?UTF-8?q?curl=E5=91=BD=E4=BB=A4=E4=B8=AD=20-d/--data=20=EF=BC=9Bfile=5Fp?= =?UTF-8?q?ut=5Fcontents=20=E5=87=BD=E6=95=B0=E8=B0=83=E7=94=A8=E5=89=8D?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E6=96=87=E4=BB=B6=E5=AD=98=E5=9C=A8=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CurlAutoLogin.php | 44 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/src/CurlAutoLogin.php b/src/CurlAutoLogin.php index 18bb197..773c00c 100644 --- a/src/CurlAutoLogin.php +++ b/src/CurlAutoLogin.php @@ -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], @@ -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}"); + } } /** @@ -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; } /** @@ -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; }