pbergman/curl-multi 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

pbergman/curl-multi

Composer 安装命令:

composer require pbergman/curl-multi

包简介

a simple curl wrapper for the multi handler

README 文档

README

This a (light) abstract layer around the curl multi for handling asynchronously request.

The purpose of this library is to make it easier to handle to handle to responses but trying to keep as close to curl as possible. This should keep the api more stable, easier to work with (when familiar with curl) and less code around curl.

The provided request is therefor not more than an array like you wold register with curl_setopt_array. So urls, header etc. should be registered with the curl methods CURLOPT_URL, CURLOPT_WRITEHEADER, CURLOPT_HTTPHEADER and rest could be found here.

example

use PBergman\CurlMulti\MultiHandler;
use PBergman\CurlMulti\Request;

$null = fopen("/dev/null", "w");
$handler = new MultiHandler(
    new Request([
        CURLOPT_URL => "http://httpstat.us/200?sleep=5000",
        CURLOPT_FILE  => $null,
    ]),
    new Request([
        CURLOPT_URL => "http://httpstat.us/404",
        CURLOPT_FILE  => $null,
    ])
);

try {
    foreach ($handler->getResponse() as $response) {
        printf(
            "request '%s' finished with code %d in %0.2fs\n",
            $response->getInfo(CURLINFO_EFFECTIVE_URL),
            $response->getInfo(CURLINFO_RESPONSE_CODE),
            $response->getInfo(CURLINFO_TOTAL_TIME)
        );
    }
} finally {
    // register cleanup
    $handler->close();
}

should give a output like:

request 'http://httpstat.us/404' finished with code 404 in 0.45s
request 'http://httpstat.us/200?sleep=5000' finished with code 200 in 5.49s

public api

__construct(RequestInterface ...$request): MultiHandler

constructor of MultiHandler accepts request so you could register your request on creating a new instance.

(new MultiHandler(
    new Request([
        CURLOPT_URL => "http://httpstat.us/200?sleep=5000",
        CURLOPT_FILE  => $null,
    ]),
    new Request([
        CURLOPT_URL => "http://httpstat.us/404",
        CURLOPT_FILE  => $null,
    ])
))->wait();
setOptions(array $options) :bool

set options for multi handler

addOption(int $key, $value) :bool

set option for multi handler

add(RequestInterface $request): MultiHandler

add request to defined instance.

defer(): void

register the close function for shutdown.

init(): void

will setup the curl multi hadle, this will be called when creating a new instance and can be useful when the close method was called.

getResponse(): \Generator|ResponseInterface[]

returns a finished request and/or block until one is finished.

wait() : array||ResponseInterface[]

wait till all request are finished

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固