Skip to content
forked from curl/curl

一个基于 curl 改造的可调用库,支持 HTTP/HTTPS、FTP/SFTP、SMTP/IMAP、WebSocket 等二十余种协议。可直接在代码而非命令行中调用,同时支持 libcurl 的全部强大功能。

License

Notifications You must be signed in to change notification settings

Pectics/curl-runner

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

curl-runner

curl logo

curl-runner 是一个基于 curl 改造的可复用库。 与原版的 curl 命令行工具或 libcurl 不同,curl-runner 让你可以在程序内部直接以函数调用的方式使用 curl 的完整功能,而无需对接 libcurl 或依赖外部的 curl.exe

简单来说,就是把 curl 从命令行工具改造成了一个 可调用的库

功能特色

  • 完全保留了 curl 的命令行参数体系
  • 支持通过 vector<string> 风格的接口在你的应用中直接调用
  • 捕获并返回 stdout / stderr 的输出
  • 基于 libcurl,具备强大而稳定的 HTTP(S)、FTP、SFTP 等协议支持
  • 提供同步执行接口,可嵌入任意 C++ 程序中
  • 可编译为静态库或动态库(.lib / .dll)

安装与构建

克隆本仓库:

git clone https://github.com/Pectics/curl-runner.git

使用 CMake 构建(示例,具体视你的环境而定):

cmake -B build -S .
cmake --build build --config Release

输出将包含:

  • curl_runner.lib / curl_runner.dll (Windows)
  • 或对应的 .a / .so (Linux)

使用示例

#include <iostream>
#include "curl_runner.h"

int main() {
    std::vector<std::string> args = {"-s", "https://example.com"};

    CurlResult result = curl_run(args);
    std::cout << "Exit code: " << result.exit_code << "\n";
    std::cout << "Stdout:\n" << result.stdout_str << "\n";
    std::cerr << "Stderr:\n" << result.stderr_str << "\n";

    return 0;
}

输出将与命令行运行如下指令一致:

curl -s https://example.com

接口说明

CurlResult 结构体

struct CurlResult {
    int exit_code;             // curl 的退出码
    std::string stdout_str;    // 捕获的标准输出
    std::string stderr_str;    // 捕获的错误输出
};

curl_run 函数

struct CurlResult curl_run(const std::vector<std::string> &args);
  • 参数与原生 curl 的 CLI 相似,不需要给定 "curl" 参数
  • 返回 CurlResult,包含执行结果

License

本项目基于 curl 原始版权声明见 LICENSE。 修改部分由 Pectics 开发,保持与原版相同的 MIT-like 协议。

联系

致谢

感谢 Daniel Stenberg 及 curl 项目的所有贡献者。 curl-runner 项目的灵感与核心功能均来自于 curl

About

一个基于 curl 改造的可调用库,支持 HTTP/HTTPS、FTP/SFTP、SMTP/IMAP、WebSocket 等二十余种协议。可直接在代码而非命令行中调用,同时支持 libcurl 的全部强大功能。

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Languages

  • C 74.8%
  • Perl 8.3%
  • M4 4.8%
  • Python 4.7%
  • CMake 2.8%
  • DIGITAL Command Language 2.2%
  • Other 2.4%