stefangabos / zebra_curl
一个高性能的解决方案,用于从您的PHP项目中使用cURL并发、异步地发出多个HTTP请求
1.6.2
2023-11-25 17:48 UTC
Requires
- php: >=5.3.0
Requires (Dev)
README
Zebra cURL 
一个高性能的解决方案,用于从您的PHP项目中发出HTTP请求。它允许并发运行多个请求,支持GET、POST、HEADER、PUT、PATCH和DELETE请求,并提供缓存、FTP下载、HTTP身份验证和代理请求的支持。
Zebra cURL 是一个高性能的cURL PHP库,它不仅允许同时运行多个异步请求,而且一旦线程完成,就可以立即处理,而无需等待队列中的其他线程完成。
此外,每次请求完成时,另一个请求将被添加到队列中,从而始终保持恒定的线程数运行,并消除繁忙等待造成的CPU周期浪费。这种方法可以更快、更高效地处理大量的cURL请求(例如一次性抓取数千个RSS源),显著减少处理时间。
该脚本支持GET(带缓存)、POST、HEADER、PUT、PATCH和DELETE请求,基本下载以及从FTP服务器下载、HTTP身份验证和通过代理服务器发出的请求。
为了最大效率,下载是流式传输的(下载的字节直接写入磁盘),消除了服务器读取文件到内存再写入磁盘的额外负担。
代码注释丰富,当PHP的错误报告级别设置为 E_ALL 时,不会产生警告/错误/通知。
特性
- 支持GET(带缓存)、POST、HEADER、PUT、PATCH和DELETE请求,基本下载以及从FTP服务器下载、HTTP身份验证和通过代理服务器发出的请求
- 允许同时运行多个请求,异步运行,一旦一个线程完成,就可以立即处理,而无需等待队列中的其他线程完成
- 下载是流式传输的(下载的字节直接写入磁盘),消除了服务器读取文件到内存再写入磁盘的额外负担
- 提供有关发出请求的详细信息
- 拥有 优秀的文档
- 代码注释丰富,当PHP的错误报告级别设置为 E_ALL 时,不会产生警告/错误/通知
📔 文档
查看 优秀的文档!
🎂 支持本项目的开发
非常感谢您的支持,这让我更有动力继续为开源项目工作。如果您喜欢这个项目,请通过点击页面顶部的星星按钮来给它加星。如果您愿意,也可以通过PayPal购买咖啡或成为赞助商。 感谢您的支持! 🎉
需求
PHP 5.3.0+,并且启用了cURL扩展。
安装
您可以通过Composer进行安装
# get the latest stable release composer require stefangabos/zebra_curl # get the latest commit composer require stefangabos/zebra_curl:dev-master
或者您可以手动安装,通过下载最新版本,解压,然后将其包含到您的项目中
require_once 'path/to/Zebra_cURL.php';
如何使用
抓取页面
<?php // include the library // (you don't need this if you installed the library via Composer) require 'path/to/Zebra_cURL.php'; // instantiate the Zebra cURL class $curl = new Zebra_cURL(); // cache results 3600 seconds $curl->cache('path/to/cache', 3600); // since we are communicating over HTTPS, we load the CA bundle from the examples folder, // so we don't get CURLE_SSL_CACERT response from cURL // you can always update this bundle from https://curl.se/docs/caextract.html $curl->ssl(true, 2, __DIR__ . '/cacert.pem'); // a simple way of scraping a page // (you can do more with the "get" method and callback functions) echo $curl->scrape('https://github.com/', true);
获取RSS源
<?php // include the library // (you don't need this if you installed the library via Composer) require 'path/to/Zebra_cURL.php'; // instantiate the Zebra cURL class $curl = new Zebra_cURL(); // cache results 3600 seconds $curl->cache('path/to/cache', 3600); // since we are communicating over HTTPS, we load the CA bundle from the examples folder, // so we don't get CURLE_SSL_CACERT response from cURL // you can always update this bundle from https://curl.se/docs/caextract.html $curl->ssl(true, 2, __DIR__ . '/cacert.pem'); $feeds = array( 'https://rss1.smashingmagazine.com/feed/' => 'Smashing Magazine', 'https://feeds.feedburner.com/nettuts' => 'TutsPlus', 'https://feeds.feedburner.com/alistapart/main' => 'A List Apart', ); // get RSS feeds of some popular tech websites $curl->get(array_keys($feeds), function($result) use ($feeds) { // everything went well at cURL level if ($result->response[1] == CURLE_OK) { // if server responded with code 200 (meaning that everything went well) // see https://httpstatus.es/ for a list of possible response codes if ($result->info['http_code'] == 200) { // the content is an XML, process it $xml = simplexml_load_string($result->body); // different types of RSS feeds... if (isset($xml->channel->item)) // show title and date for each entry foreach ($xml->channel->item as $entry) { echo '<h6>' . $feeds[$result->info['original_url']] . '</h6>'; echo '<h2><a href="' . $entry->link . '">' . $entry->title . '</a></h2>'; echo '<p><small>' . $entry->pubDate . '</small></p>'; echo '<p>' . substr(strip_tags($entry->description), 0, 500) . '</p><hr>'; } // different types of RSS feeds... else // show title and date for each entry foreach ($xml->entry as $entry) { echo '<h6>' . $feeds[$result->info['original_url']] . '</h6>'; echo '<h2><a href="' . $entry->link['href'] . '">' . $entry->title . '</a></h2>'; echo '<p><small>' . $entry->updated . '</small></p>'; echo '<p>' . substr(strip_tags($entry->content), 0, 500) . '</p><hr>'; } // show the server's response code } else trigger_error('Server responded with code ' . $result->info['http_code'], E_USER_ERROR); // something went wrong // ($result still contains all data that could be gathered) } else trigger_error('cURL responded with: ' . $result->response[0], E_USER_ERROR); });
使用自定义HTTP头部
// include the library // (you don't need this if you installed the library via Composer) require 'path/to/Zebra_cURL.php'; // instantiate the Zebra cURL class $curl = new Zebra_cURL; // since we are communicating over HTTPS, we load the CA bundle from the examples folder, // so we don't get CURLE_SSL_CACERT response from cURL // you can always update this bundle from https://curl.se/docs/caextract.html $curl->ssl(true, 2, __DIR__ . '/cacert.pem'); // set custom HTTP headers $curl->option(CURLOPT_HTTPHEADER, [ 'accept: application/json', 'X-Token-Foo-Bar: ABC123' // Pass keys to APIs, for example ]); echo $curl->scrape('https://httpbin.org/get') . PHP_EOL;
下载图片
<?php // include the library // (you don't need this if you installed the library via Composer) require 'path/to/Zebra_cURL.php'; // instantiate the Zebra cURL class $curl = new Zebra_cURL(); // since we are communicating over HTTPS, we load the CA bundle from the examples folder, // so we don't get CURLE_SSL_CACERT response from cURL // you can always update this bundle from https://curl.se/docs/caextract.html $curl->ssl(true, 2, __DIR__ . '/cacert.pem'); // download one of the official twitter image $curl->download('https://abs.twimg.com/a/1362101114/images/resources/twitter-bird-callout.png', 'cache');