Skip to content

Commit d18f03f

Browse files
author
lishipeng
committed
修改协议为BSD,并修改README
2 parents 4e59560 + 471546d commit d18f03f

File tree

5 files changed

+274
-66
lines changed

5 files changed

+274
-66
lines changed

BaseCurlHttp.php

Lines changed: 99 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
2+
23
namespace lspbupt\curl;
4+
35
use Yii;
4-
use \yii\helpers\ArrayHelper;
5-
use \yii\base\Component;
6-
use \yii\base\InvalidParamException;
7-
use Closure;
6+
use yii\helpers\ArrayHelper;
7+
use yii\base\Component;
8+
use yii\base\InvalidParamException;
9+
810
/*encapsulate normal Http Request*/
911
class BaseCurlHttp extends Component
1012
{
@@ -16,40 +18,40 @@ class BaseCurlHttp extends Component
1618
public $connectTimeout = 5;
1719
public $returnTransfer = 1;
1820
public $followLocation = 1;
19-
public $protocol = "http";
21+
public $protocol = 'http';
2022
public $port = 80;
2123
public $host;
2224
public $method = self::METHOD_GET;
2325
public $headers = array(
2426
'User-Agent' => 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.22 (KHTML, like Gecko) Ubuntu Chromium/25.0.1',
25-
'Accept-Charset' => 'GBK,utf-8' ,
27+
'Accept-Charset' => 'GBK,utf-8',
2628
);
2729
public $action;
2830
public $params;
2931
private $debug = false;
3032
//默认为非formData的模式,传文件时需要开启
3133
private $isFormData = false;
3234
private static $methodDesc = [
33-
self::METHOD_GET => "GET",
34-
self::METHOD_POST => "POST",
35-
self::METHOD_POSTJSON => "POST",
35+
self::METHOD_GET => 'GET',
36+
self::METHOD_POST => 'POST',
37+
self::METHOD_POSTJSON => 'POST',
3638
];
3739

3840
private $_curl;
3941

4042
public function init()
4143
{
4244
parent::init();
43-
if(empty($this->host)) {
44-
throw new InvalidParamException("Please config host.");
45+
if (empty($this->host)) {
46+
throw new InvalidParamException('Please config host.');
4547
}
4648
}
4749

4850
public function getUrl()
4951
{
50-
$url = $this->protocol."://".$this->host;
51-
if($this->port != 80) {
52-
$url .= ":".$this->port;
52+
$url = $this->protocol.'://'.$this->host;
53+
if ($this->port != 80) {
54+
$url .= ':'.$this->port;
5355
}
5456
return $url.$this->getAction();
5557
}
@@ -82,24 +84,24 @@ public function setMethod($method)
8284

8385
public function setGet()
8486
{
85-
if(!empty($this->headers['Content-Type'])) {
86-
unset($this->headers["Content-Type"]);
87+
if (!empty($this->headers['Content-Type'])) {
88+
unset($this->headers['Content-Type']);
8789
}
8890
return $this->setMethod(self::METHOD_GET);
8991
}
9092

9193
public function getMethod()
9294
{
93-
if(isset(self::$methodDesc[$this->method])) {
95+
if (isset(self::$methodDesc[$this->method])) {
9496
return self::$methodDesc[$this->method];
9597
}
96-
return "GET";
98+
return 'GET';
9799
}
98100

99101
public function setPost()
100102
{
101-
if(!empty($this->headers['Content-Type'])) {
102-
unset($this->headers["Content-Type"]);
103+
if (!empty($this->headers['Content-Type'])) {
104+
unset($this->headers['Content-Type']);
103105
}
104106
return $this->setMethod(self::METHOD_POST);
105107
}
@@ -110,31 +112,44 @@ public function setPostJson()
110112
return $this->setMethod(self::METHOD_POSTJSON);
111113
}
112114

113-
114115
public function setProtocol($protocol)
115116
{
116117
$this->protocol = $protocol;
117118
return $this;
118119
}
119120

121+
public function setHeaders($arr = [])
122+
{
123+
if (!ArrayHelper::isIndexed($arr)) {
124+
foreach ($arr as $key => $value) {
125+
$this->setHeader($key, $value);
126+
}
127+
}
128+
return $this;
129+
}
130+
120131
public function setHeader($key, $value)
121132
{
122-
$this->headers[$key] = $value;
133+
if ($value === null) {
134+
unset($this->headers[$key]);
135+
} else {
136+
$this->headers[$key] = $value;
137+
}
123138
return $this;
124139
}
125140

126141
private function getHeads()
127142
{
128143
$heads = [];
129-
foreach($this->headers as $key => $val) {
130-
$heads[] = $key.":".$val;
144+
foreach ($this->headers as $key => $val) {
145+
$heads[] = $key.':'.$val;
131146
}
132147
return $heads;
133148
}
134149

135150
public function getCurl()
136151
{
137-
if($this->_curl) {
152+
if ($this->_curl) {
138153
return $this->_curl;
139154
}
140155
$this->_curl = curl_init();
@@ -146,7 +161,7 @@ public function setDebug($debug = true)
146161
$this->debug = $debug;
147162
return $this;
148163
}
149-
164+
150165
public function setFormData($isFormData = true)
151166
{
152167
$this->isFormData = $isFormData;
@@ -156,8 +171,13 @@ public function setFormData($isFormData = true)
156171
public function isDebug()
157172
{
158173
return $this->debug;
159-
}
174+
}
160175

176+
public function setOpt($option, $value)
177+
{
178+
curl_setopt($this->getCurl(), $option, $value);
179+
return $this;
180+
}
161181

162182
//请求之前的操作
163183
protected function beforeCurl($params)
@@ -171,78 +191,103 @@ protected function afterCurl($data)
171191
return $data;
172192
}
173193

174-
public function httpExec($action = "/", $params = [])
194+
/**
195+
* @deprecated 推荐使用send方法
196+
*
197+
* @param string $action
198+
* @param array $params
199+
*/
200+
public function httpExec($action = '/', $params = [])
201+
{
202+
return $this->send($action, $params);
203+
}
204+
205+
public function send($action = '/', $params = [])
175206
{
176207
$this->setAction($action);
177208
$this->setParams($params);
178-
if($this->isDebug()) {
209+
if ($this->isDebug()) {
179210
echo "\n开始请求之前:\nurl:".$this->getUrl()."\n参数列表:".json_encode($this->getParams())."\n方法:".$this->getMethod()."\n";
180211
}
181212
$ret = $this->beforeCurl($params);
182-
if(!$ret) {
183-
return "";
213+
if (!$ret) {
214+
return '';
184215
}
185216
$ch = $this->getCurl();
186217
$url = $this->getUrl();
187218
if ($this->method == self::METHOD_POST) {
188219
curl_setopt($ch, CURLOPT_POST, 1);
189-
if($this->isFormData) {
220+
if ($this->isFormData) {
190221
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->getParams());
191-
}else {
222+
} else {
192223
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->getParams()));
193224
}
194225
} elseif ($this->method == self::METHOD_POSTJSON) {
195226
curl_setopt($ch, CURLOPT_POST, 1);
196227
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->getParams()));
197228
} else {
198-
if(!empty($params)) {
199-
$temp = explode("?", $url);
200-
if(count($temp) > 1) {
201-
$url = $temp[0]."?".$temp[1].'&'.http_build_query($this->getParams());
229+
if (!empty($params)) {
230+
$temp = explode('?', $url);
231+
if (count($temp) > 1) {
232+
$url = $temp[0].'?'.$temp[1].'&'.http_build_query($this->getParams());
202233
} else {
203-
$url = $url."?".http_build_query($this->getParams());
234+
$url = $url.'?'.http_build_query($this->getParams());
204235
}
205236
}
206237
}
207-
if($this->isDebug()) {
238+
if ($this->isDebug()) {
208239
echo "\n开始请求:\nurl:${url}\n参数列表:".json_encode($this->getParams())."\n方法:".$this->getMethod()."\n";
209240
}
210241
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
211242
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
212243
curl_setopt($ch, CURLOPT_URL, $url);
213244
curl_setopt($ch, CURLOPT_RETURNTRANSFER, $this->returnTransfer);
214-
curl_setopt($ch, CURLOPT_HTTPHEADER , $this->getHeads());
245+
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHeads());
215246
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->followLocation);
247+
$this->beforeCurlExec($ch);
216248
$data = curl_exec($ch);
217-
if($this->isDebug()) {
249+
$this->afterCurlExec($ch);
250+
if ($this->isDebug()) {
218251
echo "\n请求结果:".$data."\n";
219252
}
220253
$data = $this->afterCurl($data);
221254
curl_close($ch);
222-
$this->_curl = null;
255+
$this->refreshCurl();
223256
return $data;
224257
}
225258

226-
public static function requestByUrl($url, $params = [], $method=self::METHOD_GET)
259+
public function beforeCurlExec(&$ch)
260+
{
261+
}
262+
263+
public function afterCurlExec(&$ch)
264+
{
265+
}
266+
267+
public function refreshCurl()
268+
{
269+
$this->_curl = null;
270+
}
271+
272+
public static function requestByUrl($url, $params = [], $method = self::METHOD_GET)
227273
{
228274
$data = parse_url($url);
229275
$config = [];
230-
$config['protocol'] = ArrayHelper::getValue($data, "scheme", "http");
231-
$config['host'] = ArrayHelper::getValue($data, "host", "");
232-
$config['port'] = ArrayHelper::getValue($data, "port", 80);
276+
$config['protocol'] = ArrayHelper::getValue($data, 'scheme', 'http');
277+
$config['host'] = ArrayHelper::getValue($data, 'host', '');
278+
$config['port'] = ArrayHelper::getValue($data, 'port', 80);
233279
$config['method'] = $method;
234-
$action = ArrayHelper::getValue($data, "path", "");
235-
$queryStr = ArrayHelper::getValue($data, "query", "");
236-
$fragment = ArrayHelper::getValue($data, "fragment", "");
237-
if($queryStr) {
238-
$action .= "?".$queryStr;
280+
$action = ArrayHelper::getValue($data, 'path', '');
281+
$queryStr = ArrayHelper::getValue($data, 'query', '');
282+
$fragment = ArrayHelper::getValue($data, 'fragment', '');
283+
if ($queryStr) {
284+
$action .= '?'.$queryStr;
239285
}
240-
if($fragment) {
241-
$action .= "#".$fragment;
286+
if ($fragment) {
287+
$action .= '#'.$fragment;
242288
}
243289
$config['class'] = get_called_class();
244290
$obj = Yii::createObject($config);
245291
return $obj->httpExec($action, $params);
246292
}
247-
248293
}

BrowserCurl.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace lspbupt\curl;
4+
5+
/**
6+
* Class BrowserCurl
7+
* 这个Curl主要设计用来更好的模拟浏览器的行为, 可以供简单爬虫使用
8+
* 支持 加载浏览器Cookie文件 和 保存
9+
*
10+
* @package lspbupt\curl
11+
*/
12+
class BrowserCurl extends CurlHttp
13+
{
14+
/* 直接从File里加载cookie */
15+
public $cookieFile = '';
16+
17+
/*
18+
* NOTE 这个不能和cookieFile同时使用, cookieFile适用于比 cookie适用于临时性、一次Session的情况了
19+
* 从数组里加载cookie
20+
* $cookie = ['test1'=> 'cookie1', 'test2' => 'cookie110'];
21+
*/
22+
public $cookie = [];
23+
24+
/* 是否存储新的cookie, 默认会记到cookieFile里面 */
25+
public $enableCookieFileSave = false;
26+
27+
public function beforeCurlExec(&$ch)
28+
{
29+
$this->bulidCookie($ch);
30+
return parent::beforeCurlExec($ch);
31+
}
32+
33+
public function bulidCookie(&$ch)
34+
{
35+
// sending manually set cookie
36+
if ($this->cookie) {
37+
$this->setHeader('Cookie', $this->genCookieStr());
38+
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHeads());
39+
} elseif ($this->cookieFile) {
40+
// sending cookies from file
41+
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookieFile);
42+
}
43+
// 使用临时cookie的时候也可以记录到file 供下次使用
44+
if ($this->enableCookieFileSave) {
45+
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookieFile);
46+
}
47+
}
48+
49+
private function genCookieStr()
50+
{
51+
$ret = array_map(
52+
function ($key, $value) {
53+
return $key . '=' . $value;
54+
},
55+
array_keys($this->cookie),
56+
$this->cookie
57+
);
58+
return implode('; ', $ret);
59+
}
60+
}

0 commit comments

Comments
 (0)