承接 alexpw/multicurl-iterator 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alexpw/multicurl-iterator

Composer 安装命令:

composer require alexpw/multicurl-iterator

包简介

README 文档

README

This iterator class makes it simple to use PHP's multicurl effectively.

However, it does not curl_init() for you. Instead, it gives you full control over what requests you'll be making.

Install

Composer/Packagist

Basic usage

  1. You instantiate a Multicurl\Iterator instance.
  2. You give it some curl resource handles from curl_init().
  3. You start foreach'ing over the results.
$mci = new Alexpw\Multicurl\Iterator();

foreach ($curl_handles as $handle) {
	$mci->add($handle /*[, mixed $data = null] */);
}

foreach ($mci as $result) {
	doSomething($result);
}

It executes as many curl requests in parallel as you allow it. As soon as the first response is received, the foreach is allowed to continue, and you are given the $result of parsing the response.

The contents of $result

An approximation:

$result = curl_getinfo($ch);
$result['handle']   = $ch;
$result['data']     = // Optional data associated with curl handle
$result['header']   = $header_string_or_parsed_array;
$result['body']     = $body_string;
$result['errno']    = curl_errno($ch);
$result['error']    = curl_error($ch);
$result['errorstr'] = curl_errstr($ch); // when function_exists

$data, or How to ID a Request

The results are returned out of order and as soon as they are ready, but you can ID them easily.

The most convenient: $mci->add($handle, $data = null);

$data can be anything that's meaningful to you:

  • a unique id
  • a copy of the request parameters
  • whatever

Options

  • How many curl requests to execute simultaneously.

$mci->setMaxExecuting(10); // default: 10

  • Parse the response header as an array (if your request asked for headers). $mci->setParseResponseHeader(true); // default: true

  • Whether to automatically close curl handles after using them.

$mci->setCloseCurlHandles(true); // default: true

Note, if you want to be able to reuse curl handles for retries or whatever, you'll need to manage and close them yourself.

Real world usage

You don't need to create all of your curl handles in advance, they can be added at any time.

  1. Setup your data
  2. You instantiate a Multicurl\Iterator instance.
  3. You configure options.
  4. You give it some curl resource handles from curl_init().
  5. You start foreach'ing over the results.
$article_ids       = array(1, 2, 3, 4 /*,...*/);
$article_id_chunks = array_chunk($article_ids, 50);

$curr_chunk   = 0;
$total_chunks = count($article_id_chunks);

function addArticlesToIterator($mci, $chunk)
{
	foreach ($chunk as $article_id) {
		$ch = initCurlHandleForArticle($article_id);
		$mci->add($ch, $article_id);
	}
}

$mci = new Alexpw\Multicurl\Iterator();
$mci->setMaxExecuting(6);

addArticlesToIterator($mci, $article_id_chunks[$curr_chunk++]);

foreach ($mci as $result) {
	if ($mci->getCountPendingRequests() < 10 &&
		$curr_chunk < $total_chunks) {
		addArticleRequests($mci, $article_id_chunks[$curr_chunk++]);
	}

	if ($result['errno'] !== 0) {
		logFailed($result);
	} elseif ($result['http_code'] === 408) {
		$ch = initCurlHandleForRetry($result);
		$mci->add($ch);
    } else {
		doSomething($result);
    }
}

License

Multicurl Iterator is licensed under the MIT License - see the LICENSE file for details.

统计信息

  • 总下载量: 66
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2014-09-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固