1 Star 2 Fork 3

何人曾道 / curlmulti

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

关于

这是目前最好的php curl类库,很多开发者基于此库开发项目。类库是对curl_multi_*系列函数的封装,性能、扩展性、易用性都是最高水平,很强大。

需求

PHP 5.4 +

安装

composer require phpdr.net/php-curlmulti:2.*

联系我们

Email: admin@phpdr.com
QQ群:215348766

特性

  1. 极低的CPU和内存使用率。
  2. 速度在程序层面最高(实测抓取html速度达到2000+页每秒,下载速度1000Mbps)。
  3. 支持全局并发设置和根据任务类型单独设置并发。
  4. 支持状态回调,运行中的所有信息都被返回,包括单独的每个任务信息。
  5. 支持通过回调添加任务。
  6. 支持用户自定义回调,可以在回调中做任何事情。
  7. 支持成功回调返回值控制任务。
  8. 支持全局错误回调和单独任务的错误回调,所有和错误相关的信息都被返回。
  9. 支持内部全自动重试。
  10. 支持用户参数任意传递。
  11. 支持CURLOPT_*全局设置和单个任务设置。
  12. 强大的内置缓存,可以设置全局缓存和单独任务缓存。
  13. 所有配置可以在运行中动态改变并生效!
  14. 基于此库你可以开发各种强劲的CURL应用。

运行机制

没有pthreads扩展支持,php是单线程顺序执行的,所以本类库大量使用回调函数。类库只有两个常用的方法,add()和start(),add()添加一个任务到内部任务池,start()开始以$maxTrhead设置的并发数进行回调循环,此方法是阻塞的直到所有任务完成。如果有大量的任务需要处理,使用$cbTask指定添加任务的回调函数,当并发不足并且任务池为空时此回调函数被调用。当一个任务完成之后add()中指定的回调立刻被执行,然后curl从任务池取一个任务添加到并发请求中。所有任务完成后start()函数结束。

文件

src/Core.php
核心库。

src/Base.php
核心库的封装,包含非常有用的工具和一些规范。

src/Exception.php
异常。

src/AutoClone.php
一个完美的全自动网站克隆工具。

特性:

  1. 软件工程,面向对象和编程技巧的完美结晶,和php-curlmulti一样非常具有艺术性。
  2. 使用方便,只有一个无参方法start()。
  3. 低耦合,扩展极其容易,配合php-curlmulti的强大能力可以分分钟拷贝一个站。
  4. 所有页面的重复的url只会精确处理一次。
  5. 数学层面100%全自动处理任意格式url的相对路径绝对路径,100%精确!
  6. css中引入的背景图等资源全自动处理,css中的@import全自动处理,支持任意深度!
  7. 支持指定多个前缀url并且可以针对每个前缀url设置一个深度。
  8. 支持对每个前缀url指定二级前缀,每个二级前缀还可以设置一个深度。
  9. 全自动处理3xx跳转。
  10. 跨站资源共享,例如采集站点A的时候A用到了站点B的图片,jss,css等,等采集站点B的时候这些文件会直接使用不会再次下载。
  11. 同一个目录下可以复制任意数量的站点并且不会发生任何可能的文件重复或覆盖。
  12. 支持不同类型资源文件下载控制。

存在的问题:

1. 针对IE浏览器的注释性css不会处理,因为还没找到一种符合标准的处理方式,这个问题造成的影响可以忽略不计。

结果展示:http://manual.phpdr.net/

API(Core)

public $maxThread = 10

最大并发数,这个值可以运行中动态改变。
最大数限制跟操作系统和libcurl有关,和本库无关。

public $maxThreadType = array ()

为不同类型的任务设置单独的并发数,数组的键是类型(在add()中指定),值是并发数。不同类型的的并发数综合可以超过$maxThread。无类型任务的并发数是$maxThread减去所有类型的综合。无类型任务并发数小于零的话会被设置为零,这意味着无类型任务不会被执行除非运行中动态改变设置。

public $maxTry = 3

触发curl错误或用户错误之前最大重试次数,超过次数$cbFail指定的回调会被调用。

public $opt = array ()

全局CURLOPT_*,可以被add()中设置的opt覆盖。

public $cache = array ('enable' => false, 'enableDownload'=> false, 'compress' => false, 'dir' => null, 'expire' =>86400, 'dirLevel' => 1, 'verifyPost' => false, 'overwrite' => false, 'overwriteExpire' => 86400)

缓存选项很容易被理解,缓存使用url来识别。如果使用缓存类库不会访问网络而是直接返回缓存。

public $taskPoolType = 'queue'

有两个值stack或queue,这两个选项决定任务池是深度优先还是广度优先,默认是stack深度优先。

public $cbTask = array(0=>'callback',1=>'callback param')

当并发数小于$maxThread并且任务池为空的时候类库会调用$cbTask指定的回调函数。$cbTask[0]是回调函数,$cbTask[1]是传递给回调函数的参数。

public $cbInfo = null

运行信息回调函数,回调中使用print_r()可以查看详细信息,回调函数最快1秒钟调用一次。

public $cbUser = null

用户自定义回调函数,这个函数调用非常频繁,用户函数可以执行任何操作。

public $cbFail = null

失败任务回调,可以被add()中指定的错误回调覆盖。

public function __construct()

子类必须调用此函数。

public function add(array $item, $process = null, $fail = null)

添加一个任务到任务池
$item['url'] 不能为空。
$item['opt']=array() 当前任务的CURLOPT_*,覆盖全局的CURLOPT_*。
$item['args'] 成功和失败回调的第二个参数。
$item['ctl']=array() 一些额外的控制项
$item['ctl']['type'] 任务类型,用在$maxThreadType属性。
$item['ctl']['cache']=array() 任务缓存配置,覆盖合并$cache属性。
$item['ctl']['ahead'] 忽略$taskPoolType属性,此种类型的任务总是被优先加入并发中。
$process 任务成功完成调用此回调,回调的第一个参数是结果数组,第二个参数是$item['args']。
$fail 任务失败回调,第一个参数是相关信息,第二个参数是$item['args']。

public function start($persist=null)

开始回调循环,此方法是阻塞的。 参数$persist是一个回调函数,如果返回true表示当所有任务完成后继续保持start()为阻塞,如果需要sleep必须在回调中完成。

API(Base)

function __construct($curlmulti = null)

使用默认的核心类或使用自行定义的子类或对象。

function hashpath($name, $level = 2)

获得hash相对路径,每个目录最大文件数是4096。

function substr($str, $start, $end = null, $mode = 'g')

获取开始和结束字符串之间的字符串,开始和结束字符串不包含在内。

function cbCurlFail($error, $args)

全局默认错误回调。

function cbCurlInfo($info,$isFirst,$isLast)

默认的信息回调,以标准形式输出运行信息。

function encoding($html, $in = null, $out = 'UTF-8', $mode = 'auto')

强力的转码函数,可以自动获取当前编码,转码后自动修改<head></head>中的编码,可选不同的转码函数。

function isUrl($str)

是否是一个绝对的url。

function uri2url($uri, $urlCurrent)

根据当前页面url获取当前页中的相对uri对应的绝对url。

function url2uri($url, $urlCurrent)

根据当前页面url获取当前页中的绝对url对应的相对uri。

function urlDir($url)

绝对url对应的目录,参数中的url应该是重定向之后的url。

function getCurl()

返回核心类的对象。

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright {yyyy} {name of copyright owner} Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

Undoubtedly the best php curl library.It's power beyond imagination. 展开 收起
PHP
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/yil/curlmulti.git
git@gitee.com:yil/curlmulti.git
yil
curlmulti
curlmulti
master

搜索帮助