khr/php-mcurl-client

为PHP 5.3封装curl客户端(http客户端);使用php多curl,并行请求并编写异步代码

3.1.0 2016-06-08 07:34 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:12:59 UTC


README

Build Version License Downloads

特性

  • PHP >= 5.3 (兼容至版本 7.0 && hhvm)
  • 稳定. 许多项目都在使用
  • 快速 请求。最小开销
  • 单行运行查询
  • 并行请求(多请求)。默认启用并行请求
  • 使用异步请求
  • 请求均衡
  • 无调用

安装

推荐通过 composer 安装多curl。

$ composer require khr/php-mcurl-client:3.*
{
    "require": {
        "khr/php-mcurl-client": "~3.0"
    }
}

快速开始和示例

创建

use MCurl\Client;
$client = new Client();

简单请求

echo $client->get('http://example.com');

检查错误

$result = $client->get('http://example.com');
echo (!$result->hasError()
    ? 'Ok: ' . $result
    : 'Error: ' .$result->error . ' ('.$result->errorCode.')')
    , PHP_EOL;

在请求中添加curl选项

echo $client->get('http://example.com', [CURLOPT_REFERER => 'http://example.net/']);

POST请求

echo $client->post('http://example.com', ['post-key' => 'post-value'], [CURLOPT_REFERER => 'http://example.net/']);

简单并行请求

// @var $results Result[]
$results = $client->get(['http://example.com', 'http://example.net']);
foreach($results as $result) {
    echo $result;
}

并行请求

$urls = ['http://example.com', 'http://example.net', 'http://example.org'];
foreach($urls as $url) {
    $client->add([CURLOPT_URL => $url]);
}
// wait all request
// @var $results Result[]
$results = $client->all();

并行请求;只等待下一个结果

$urls = ['http://example.com', 'http://example.net', 'http://example.org'];
foreach($urls as $url) {
    $client->add([CURLOPT_URL => $url]);
}
while($result = $client->next()) {
    echo $result;
}

动态添加请求

while($result = $client->next()) {
    $urls = fun_get_urls_for_parse_result($result);
    foreach($urls as $url) {
        $client->add([CURLOPT_URL => $url]);
    }
    echo $result;
}

非阻塞请求;使用异步代码;仅运行请求并检查完成

while($client->run() || $client->has()) {
    while($client->has()) {
        // no blocking
        $result = $client->next();
        echo $result;
    }

    // more async code

    //end more async code
}

使用参数

$result = $client->add([CURLOPT_URL => $url], ['id' => 7])->next();
echo $result->params['id']; // echo 7

结果

// @var $result Result
$result->body; // string: body result
$result->json; // object; @see json_encode
$result->getJson(true); // array; @see json_encode
$result->headers['content-type']; // use $client->enableHeaders();
$result->info; // @see curl_getinfo();
$result->info['total_time']; // 0.001

$result->hasError(); // not empty curl_error or http code >=400
$result->hasError('network'); // only not empty curl_error
$result->hasError('http'); // only http code >=400
$result->getError(); // return message error, if ->hasError();
$result->httpCode; // return 200

配置

Client::setOptions

此curl选项添加到所有请求中

$client->setOptions([CURLOPT_REFERER => 'http://example.net/']);

Client::enableHeaders

添加到结果中的头部信息

$client->enableHeaders();

Client::setMaxRequest

并行执行查询的最大数量

$client->setMaxRequest(20); // set 20 parallel request

Client::setSleep

使用方法 $client->setSleep 来在时间间隔内平衡请求。这可以帮助你通过调整转换率来避免动态内容接收时的压力。示例

$client->setSleep (20, 1);

1秒内最多运行20个查询。

对于静态内容,建议限制下载速度,以免占用过多带宽。示例

//channel 10 Mb.
$client->setMaxRequest (123);
$client->setOptions([CURLOPT_MAX_RECV_SPEED_LARGE => (10 * 1024 ^ 3) / 123]);

食谱

下载文件

$client->get('http://exmaple.com/image.jpg', [CURLOPT_FILE => fopen('/tmp/image.jpg', 'w')]);

节省内存

为了减少内存使用,你可以将查询结果写入临时文件。

$client->setStreamResult(Client::STREAM_FILE); // All Result write in tmp file.
/**
 * @see tests/ and source
 */