Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:ares333/php-curl.git
Browse files Browse the repository at this point in the history
  • Loading branch information
ares333 committed Dec 12, 2018
2 parents fc8fae9 + 2d57aee commit 17f4b5e
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 27 deletions.
6 changes: 1 addition & 5 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ composer require ares333/php-curl
```

## 联系我们
QQ群:

2️⃣ 744854777

1⃣️ 215348766(满)
QQ群:215348766

## 特性
1. 极低的CPU、内存使用率和高性能(实测抓取html速度达到3000+页每秒,下载速度1000Mbps)。
Expand Down
1 change: 1 addition & 0 deletions demo/auto_cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require_once '_inc.php';
use Ares333\Curl\Toolkit;
$toolkit = new Toolkit();
$toolkit->setCurl();
$curl = $toolkit->getCurl();
$curl->maxThread = 2;
$curl->cache['enable'] = true;
Expand Down
1 change: 1 addition & 0 deletions demo/basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require_once '_inc.php';
use Ares333\Curl\Toolkit;
$toolkit = new Toolkit();
$toolkit->setCurl();
$curl = $toolkit->getCurl();
$curl->onInfo = null;
$responseCode = null;
Expand Down
1 change: 1 addition & 0 deletions demo/chained_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require_once '_inc.php';
use Ares333\Curl\Toolkit;
$toolkit = new Toolkit();
$toolkit->setCurl();
$curl = $toolkit->getCurl();
$curl->onInfo = null;
$url = 'http://baidu.com';
Expand Down
1 change: 1 addition & 0 deletions demo/download.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require '_inc.php';
use Ares333\Curl\Toolkit;
$toolkit = new Toolkit();
$toolkit->setCurl();
$curl = $toolkit->getCurl();
$curl->onInfo = null;
$url = 'http://www.baidu.com/img/bd_logo1.png';
Expand Down
1 change: 1 addition & 0 deletions demo/http_clone.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function onProcess($r, $args)
}
}
$clone = new HttpCloneDemo($dir);
$clone->setCurl();
$clone->getCurl()->opt[CURLOPT_CONNECTTIMEOUT] = 3;
$clone->getCurl()->opt[CURLOPT_ENCODING] = 'gzip,deflate';
$clone->getCurl()->cache['enable'] = true;
Expand Down
1 change: 1 addition & 0 deletions demo/task_callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require_once '_inc.php';
use Ares333\Curl\Toolkit;
$toolkit = new Toolkit();
$toolkit->setCurl();
$curl = $toolkit->getCurl();
$curl->maxThread = 1;
$curl->onTask = function ($curl) {
Expand Down
51 changes: 36 additions & 15 deletions src/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,7 @@ public function start()
if (! isset($task['opt'][CURLOPT_FILE])) {
$param['body'] = curl_multi_getcontent($ch);
if (isset($task['opt'][CURLOPT_HEADER])) {
preg_match_all("/HTTP\/.+(?=\r\n\r\n)/Usm", $param['body'], $param['header']);
$param['header'] = $param['header'][0];
$pos = 0;
foreach ($param['header'] as $v) {
$pos += strlen($v) + 4;
}
$param['body'] = substr($param['body'], $pos);
$param = array_merge($param, $this->parseResponse($param['body']));
}
}
}
Expand Down Expand Up @@ -311,6 +305,19 @@ public function start()
$this->_mh = null;
}

public function parseResponse($response)
{
$res = [];
preg_match_all("/HTTP\/.+(?=\r\n\r\n)/Usm", $response, $res['header']);
$res['header'] = $res['header'][0];
$pos = 0;
foreach ($res['header'] as $v) {
$pos += strlen($v) + 4;
}
$res['body'] = substr($response, $pos);
return $res;
}

/**
* Call $this->onInfo
*
Expand Down Expand Up @@ -440,6 +447,26 @@ protected function onProcess($task, $param)
}
}

/**
*
* @param string $url
* @param string|array $post
* @return string
*/
public function getCacheFile($url, $post = null)
{
$suffix = '';
if (isset($post)) {
if (is_array($post)) {
$post = http_build_query($post);
ksort($post);
}
$suffix .= $post;
}
$key = md5($url . $suffix);
return substr($key, 0, 3) . '/' . substr($key, 3, 3) . '/' . substr($key, 6);
}

/**
* Set or get file cache.
*
Expand All @@ -458,18 +485,12 @@ protected function cache($task, $data = null)
return;
}
$url = $task['opt'][CURLOPT_URL];
// verify post
$suffix = '';
$post = null;
if (true == $config['verifyPost'] && ! empty($task['opt'][CURLOPT_POSTFIELDS])) {
$post = $task['opt'][CURLOPT_POSTFIELDS];
if (is_array($post)) {
$post = http_build_query($post);
}
$suffix .= $post;
}
$key = md5($url . $suffix);
$file = rtrim($config['dir'], '/') . '/';
$file .= substr($key, 0, 3) . '/' . substr($key, 3, 3) . '/' . substr($key, 6);
$file .= $this->getCacheFile($url, $post);
if (! isset($data)) {
if (file_exists($file)) {
$time = time();
Expand Down
14 changes: 7 additions & 7 deletions src/HttpClone.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function __construct($dir)
*/
function add($url, $depth = null)
{
$url = $this->urlFormater($url);
$url = $this->formatUrl($url);
if (! isset($url)) {
user_error('invalid url(' . $url . ')', E_USER_ERROR);
}
Expand All @@ -87,9 +87,9 @@ function add($url, $depth = null)
*
* @see \Ares333\Curl\Toolkit::formatUrl()
*/
function urlFormater($url)
function formatUrl($url)
{
$url = parent::urlFormater($url);
$url = parent::formatUrl($url);
$parse = parse_url($url);
if (! isset($parse['path'])) {
$parse['path'] = '/';
Expand All @@ -103,7 +103,7 @@ function urlFormater($url)
function start()
{
foreach ($this->blacklist as $k => $v) {
$this->blacklist[$k] = $this->urlFormater($v);
$this->blacklist[$k] = $this->formatUrl($v);
}
foreach (array_keys($this->_task) as $v) {
if ($this->checkUrl($v)) {
Expand Down Expand Up @@ -139,7 +139,7 @@ function start()
*/
protected function url2src($url, $urlCurrent, $isLocal, $fixQuery = true)
{
$url = $this->urlFormater($url);
$url = $this->formatUrl($url);
if (in_array($url, $this->blacklist)) {
return '';
}
Expand Down Expand Up @@ -295,7 +295,7 @@ function onProcess($r, $args)
continue;
}
$url = $this->uri2url($href, $urlCurrent);
if ($this->isProcess($this->urlFormater($url))) {
if ($this->isProcess($this->formatUrl($url))) {
if (in_array(pathinfo($url, PATHINFO_EXTENSION),
$this->downloadExtension)) {
$urlDownload[$url] = array();
Expand Down Expand Up @@ -332,7 +332,7 @@ function onProcess($r, $args)
$urlParse
) as $k => $v) {
foreach ($v as $k1 => $v1) {
$k1 = $this->urlFormater($k1);
$k1 = $this->formatUrl($k1);
if ($this->checkUrl($k1)) {
$file = $this->url2file($k1);
if (null == $file) {
Expand Down

0 comments on commit 17f4b5e

Please sign in to comment.