承接 calabashdoll/curl-future 相关项目开发

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

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

calabashdoll/curl-future

Composer 安装命令:

composer require calabashdoll/curl-future

包简介

Parallel CURL Requests with PHP

README 文档

README

multicurl系列方法是提高php请求后端http接口的一种途径。但是直接使用的话,存在几方面问题:

  • 部分版本的curl扩展有bug,需要用特定的方式来调用(Rolling cURL: PHP并发最佳实践)
  • 网上流传的CurlRolling库都只支持前面加入,最后一并执行这种使用模式。而最理想的是随时加入,需要的时候从里面取出所需的结果,且不需等待其他请求返回
  • 为了提升效率,大部分库选择使用回调函数的方式来执行,对已有程序改造成本较高

为了解决这些问题,开发了CurlFuture库,实现了并行请求,先到先取,链式执行的特性。

应用场景

对于一些大型公司,PHP作为接口聚合层来使用,而接口通过HTTP协议给出。对于一些复杂的页面,可能需要请求几十个相互独立的接口, 如果使用并行模式,则可以极大的提升性能。

安装方法

引入入口php文件即可:include __DIR__.'/curl_future.php';

使用方法

/**
 * 获得一个延迟执行curl的类
 * @param $url 请求url地址
 * @param $options = array(), 
 *		header:头信息(Array), 
 *		proxy_url:代理服务器地址, 
 *		timeout:超时时间,可以小于1
 *		post_data: string|array post数据
 * @return CurlFuture\HttpFuture
 */
function curl_future($url, $options = array());

echo curl_future("http://s.newhua.com/2015/1113/304528.shtml?4", array())
		->fetch();

并行请求的实例(async.php)

include __DIR__.'/curl_future.php';

$f4 = curl_future("http://s.newhua.com/2015/1113/304528.shtml?4");
$f5 = curl_future("http://s.newhua.com/2015/1113/304528.shtml?5");

echo strlen($f1->fetch());	//这个地方会并行执行
echo "\n";
echo strlen($f2->fetch());
echo "\n";

链式执行的示例(then.php)

include __DIR__.'/curl_future.php';

echo curl_future("http://s.newhua.com/2015/1113/304528.shtml")
	->then(function($data){
		return strlen($data);
	})
	->then(function($len){
		return "Length: $len";
	})
	->fetch();

和Model/Service结合的示例(model.php)

include __DIR__.'/curl_future.php';

class BookModel{
	//接口串行调用的示例,通过then函数将处理过程串联起来
	static public function getTitleFuture($id){
		return curl_future("http://111.202.7.252/{$id}")
			->then(function($data){
				return strlen($data);
			})
			->then(function($data){
				$url = "http://111.202.7.252/{$data}";
				$html = curl_future($url)->fetch();
				preg_match('/title(.+?)\/title/is', $html, $matches);
				return $matches[1];
			});
	}
	
	//普通接口调用+后续处理的示例
	static public function getContentFuture($id){
		return curl_future("http://111.202.7.252/{$id}")
				->then(function($data){
					return substr($data, 0, 100);
				});
	}
}

//多个请求并行发出示例,这个地方用Model封装起来,便于和不同框架相结合
$t1 = BookModel::getTitleFuture('111');
$t2 = BookModel::getTitleFuture('222');
$t3 = BookModel::getTitleFuture('333');

$c1 = BookModel::getContentFuture('111');
$c2 = BookModel::getContentFuture('222');
$c3 = BookModel::getContentFuture('333');

//fetch函数会阻塞住,这个地方会把所有队列里面的请求发出,直到需要获取的t1的请求执行完再返回
var_dump($t1->fetch());
//由于上个fetch已经阻塞过了,下面的这个fetch很可能无需阻塞直接返回,也有可能上面的fetch没有执行完,此处阻塞住继续执行请求,直到拿到t2的数据
var_dump($t2->fetch());
var_dump($c3->fetch());

原理

在每次fetch的时候,开始事件循环。当所需http返回后,结束循环。继续执行php逻辑。

	//task_manager.php
	public function fetch($ch){
		$chKey = (int)$ch;

		//如果两个队列里面都没有,那么退出
		if(!array_key_exists($chKey, $this->runningTasks) && !array_key_exists($chKey, $this->finishedTasks) )return false;
	
		$active = 1;
		do{
			//如果任务完成了,那么退出
			if(array_key_exists($chKey, $this->finishedTasks))break;

			//执行multiLoop,直到该任务完成
			$active = $this->multiLoop();
			//如果执行出错,那么停止循环
			if($active === false)break;
		}while(1);
		
		return $this->finishTask($ch);
	}

性能测试

请求本机接口200次,nginx默认页面,同步、异步与file_get_contents对比

/example/bench.php

curl_future sync:384 ms
file_get_contents:390 ms
curl_futhre async:68 ms

curl_future sync:624 ms
file_get_contents:460 ms
curl_futhre async:69 ms

curl_future sync:463 ms
file_get_contents:355 ms
curl_futhre async:70 ms

curl_future sync:447 ms
file_get_contents:409 ms
curl_futhre async:66 ms

同步方式没有file_get_contents稳定,但是异步批量方式性能提升很明显。

参考项目

calabashdoll/curl-future 适用场景与选型建议

calabashdoll/curl-future 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 37 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 09 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 calabashdoll/curl-future 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 calabashdoll/curl-future 我们能提供哪些服务?
定制开发 / 二次开发

基于 calabashdoll/curl-future 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-09-27