thlib/php-curl-multi-basic 问题修复 & 功能扩展

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

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

thlib/php-curl-multi-basic

Composer 安装命令:

composer require thlib/php-curl-multi-basic

包简介

Basic implementation of PHP curl_multi. Minimal code, easy to configure, better memory management, no double loop, no 100% CPU usage error.

README 文档

README

Basic implementation of PHP curl multi

There are many implementations of curl multi, but the majority suffer from high CPU usage, inefficient memory management and run only after each request has finished instead of immediately.

Here is an example of how to avoid all of those issues without overloading it with functionality.

require 'CurlMulti.php';

$handles = [
    [
        // regular url
        CURLOPT_URL=>"http://example.com/",
        CURLOPT_FOLLOWLOCATION=>false,
        CURLOPT_WRITEFUNCTION=>function($ch, $body)
        {
            print $body;
            return strlen($body);
        }
    ],
     [
        // invalid url
        CURLOPT_URL=>"httpzzz://example.com/",
        CURLOPT_FOLLOWLOCATION=>false,
        CURLOPT_WRITEFUNCTION=>function($ch, $body)
        {
            print $body;
            return strlen($body);
        }
    ],
    [
        // url with a redirect
        CURLOPT_URL=>"http://www.php.net",
        CURLOPT_FOLLOWLOCATION=>false,
        CURLOPT_HEADERFUNCTION=>function($ch, $header)
        {
            print "header from http://www.php.net: ".$header;
            return strlen($header);
        },
        CURLOPT_WRITEFUNCTION=>function($ch, $body)
        {
            print $body;
            return strlen($body);
        }
    ]
];



//create the multiple cURL handle
$CurlMulti = new CurlMulti();

foreach($handles as $opts) {
    // create cURL resources
    $ch = curl_init();

    // set URL and other appropriate options
    curl_setopt_array($ch, $opts);

    // add the handle
    $CurlMulti->add($ch);
}

$statusCode = $CurlMulti->run(function($ch, $statusCode) {
    $info = curl_getinfo($ch);

    if ($statusCode !== CURLE_OK) {
        // handle the error somehow
        print "Curl handle error: ".curl_strerror($statusCode)." for ".$info['url'].PHP_EOL;
        return;
    }

    //$body = curl_multi_getcontent($ch);
    //print $body;
});
if ($statusCode !== CURLM_OK) {
    print "Curl multi handle error: ".curl_multi_strerror($statusCode)." for ".$info['url'].PHP_EOL;
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unlicense
  • 更新时间: 2020-04-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固