Skip to content

Commit

Permalink
支持添加全局opt,方便使用代理或者其他自定义opt
Browse files Browse the repository at this point in the history
  • Loading branch information
Zjmainstay committed Dec 27, 2018
1 parent e54ec02 commit 7eb9a03
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/CurlAutoLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class CurlAutoLogin {
protected $lockedLastCookieFile = false;
//日志路径
protected $logPath = '';
//全局opt
protected $globalOpts = [];

public function __construct($logPath = '') {
if(!empty($logPath) && is_writable($logPath)) {
Expand All @@ -24,6 +26,15 @@ public function __construct($logPath = '') {
}
}

/**
* 设置全局请求opt(方便使用代理之类的请求)
* @param $opts
*/
public function setGlobalOpts($opts = [])
{
$this->globalOpts += $opts;
}

/**
* 根据curl信息执行并解析结果
* @param string $curlContent 利用Firefox浏览器复制cURL命令
Expand All @@ -33,12 +44,13 @@ public function __construct($logPath = '') {
*/
public function execCurl($curlContent, $callbackBefore = false, $callbackAfter = false) {
$parseCurlResult = $this->parseCurl($curlContent);
if(!empty($callbackBefore)) {
if(is_callable($callbackBefore)) {
$parseCurlResult = $callbackBefore($parseCurlResult);
}
$parseCurlResult['opt'] += $this->globalOpts;
$execCurlResult = $this->_execCurl($parseCurlResult);

if(!empty($callbackAfter)) {
if(is_callable($callbackAfter)) {
$execCurlResult = $callbackAfter($parseCurlResult, $execCurlResult);
}

Expand All @@ -55,7 +67,7 @@ public function execCurl($curlContent, $callbackBefore = false, $callbackAfter =
public function execCurlWithCookie($curlContent, $callbackBefore = false, $callbackAfter = false) {
return $this->execCurl($curlContent, function($parseCurlResult) use ($callbackBefore) {
$parseCurlResult['header'][] = $parseCurlResult['cookie'];
if(!empty($callbackBefore)) {
if(is_callable($callbackBefore)) {
$parseCurlResult = $callbackBefore($parseCurlResult);
}
return $parseCurlResult;
Expand Down Expand Up @@ -154,7 +166,7 @@ protected function _execCurl($parseCurlResult) {
//extend opt
if(!empty($parseCurlResult['opt'])) {
foreach ($parseCurlResult['opt'] as $key => $value) {
curl_setopt($ch,$key, $value);
curl_setopt($ch, $key, $value);
}
}

Expand Down

0 comments on commit 7eb9a03

Please sign in to comment.