嵌入式 Linux下curl库API简单介绍

简介: 1:CURLcode curl_global_init(long flags);     这个函数全局需要调用一次(多次调用也可以,不过没有必要), 所以这也是把Curlplus设计成单体类的原因,curl_global_init函数在其他libcurl函数调用前至少调用一次,程序最后需要调用curl_global_cleanup,进行清理。

1:CURLcode curl_global_init(long flags);

    这个函数全局需要调用一次(多次调用也可以,不过没有必要), 所以这也是把Curlplus设计成单体类的原因,curl_global_init函数在其他libcurl函数调用前至少调用一次,程序最后需要调用curl_global_cleanup,进行清理。

参数:flags 

CURL_GLOBAL_ALL Initialize everything possible. This sets all known bits.

CURL_GLOBAL_SSL Initialize SSL

CURL_GLOBAL_WIN32 Initialize the Win32 socket libraries.

CURL_GLOBAL_NOTHING Initialise nothing extra. This sets no bit.

 

CURLcode 是一个enum,当CURLcode为CURLE_OK时,表示函数执行成功,否则失败,具体错误原因可以查看<curl/curl.h>文件内的定义。

 

2:curl_easy_init() - Start a libcurl easy session

curl_easy_init用来初始化一个CURL的指针(有些像返回FILE类型的指针一样). 相应的在调用结束时要用curl_easy_cleanup函数清理. 一般curl_easy_init意味着一个会话的开始. 它的返回值是CURL *,curl_easy_init函数是线程相关的,也就是说不能在一个线程中调用另外一个线程通过curl_easy_init创建的CURL指针。

 

3:CURLcode curl_easy_setopt(CURL *handle, CURLoption option, parameter); 

描述: 这个函数最重要了.几乎所有的curl 程序都要频繁的使用它.它告诉curl库.程序将有如何的行为. 比如要查看一个网页的html代码等.,要想具体了解CURL的行为,必须对CURLoption有足够的了解,具体可以参考:

http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

 

这里有两个类型不易理解CURLOPT_WRITEFUNCTION,CURLOPT_WRITEDATA

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Curlplus::writer);

 

设置一个回调函数,这个回调函数的格式是

size_t function( void *ptr, size_t size, size_t nmemb, void *stream);

ptr,返回数据的指针

size,返回数据每块的大小

nmemb,返回数据的块数(这里返回数据串的真正大小为size*nmemb)

stream,是curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); 中的buffer的指针。

在上面的例子中,buffer设置为一个string对象,所以,在回调函数writer中有了writerData->append(data, len); 

 

4:CURLcode curl_easy_perform(CURL *handle);

执行远程请求

 

参考资料

http://curl.haxx.se/

http://curl.haxx.se/lxr/source/docs/examples/

 

基于curl 的C API写了一个扩展C++ singleton类(当然curl也有C++ API),这个单体类只是对HTTP请求做了简单封装,提供post,get方法,并得到请求url内的返回值(保存到string对象中),也很容易扩展到其他协议上去。

Curlplus.h文件

#ifndef _CURLPLUS_H__

#define _CURLPLUS_H__ 

 

#ifndef __CURL_CURL_H

#include <curl/curl.h>

#endif

 

#ifndef __CURL_EASY_H

#include <curl/easy.h>

#endif

 

#include <memory>

#include <string> 

 

using namespace::std; 

 

namespace CP

{

    class Curlplus

    {

    public:

        static  Curlplus& get_instance();

        string post(const string& url,const string& content) const;

        string get(const string& url) const;

    protected:

        Curlplus(void);

        ~Curlplus(void);

        Curlplus(const Curlplus&);

        Curlplus& operator=(const Curlplus&);

        static int writer(char *data, size_t size, size_t nmemb,std::string *writerData);

    private:

        static auto_ptr<Curlplus> _instance;

        inline void _setCurlopt(CURL *curl,const string& url) const;

        // default timeout 300s

        static const int _defaulttimeout = 300;

        static string buffer;

        friend class auto_ptr<Curlplus>;

    };

}

 

#endif 

 

Curlpuls.cc文件 

 

#ifndef SPIVOT_CURLPLUS_H__

#include "Curlplus.h"

#endif 

 

using namespace std;

using namespace CP;

 

 

auto_ptr<Curlplus> Curlplus::_instance;

string Curlplus::buffer;

static char errorBuffer[CURL_ERROR_SIZE]; 

 

Curlplus& Curlplus::get_instance()

{

    if(_instance.get() == NULL)

    {

        _instance.reset(new Curlplus());

    } 

 

    return *_instance.get();

}

 

int Curlplus::writer(char *data, size_t size, size_t nmemb, string *writerData)

{

    if (writerData == NULL)

        return 0;

    int len = size*nmemb;

    writerData->append(data, len);

 

    return len;

}

 

Curlplus::Curlplus(void)

 

    CURLcode code = curl_global_init(CURL_GLOBAL_ALL);

    if(code != CURLE_OK)

    {

        cout << "curl_init failed, error code is: " << code;

    }

}

 

Curlplus::~Curlplus(void)

{

    curl_global_cleanup();

 

string Curlplus::post(const string& url, const string& content) const

{

    buffer="";

    CURL *curl = curl_easy_init();

    if(curl == NULL)

    {

        cout << "curl_easy_init failed ";

    }  

 

    _setCurlopt(curl,url);

    curl_easy_setopt(curl, CURLOPT_POST, 1 );

    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content.c_str());   

 

    CURLcode code = curl_easy_perform(curl);

    if(code != CURLE_OK)

    {

        cout << "curl_easy_perform failed: "<< code;

    }

    curl_easy_cleanup(curl);

 

    return buffer;

 

string Curlplus::get(const string& url) const

{

    buffer="";

    CURL *curl = curl_easy_init();

    if(curl == NULL)

    {

        cout << "curl_easy_init failed ";

    }  

 

    _setCurlopt(curl,url);

    CURLcode code = curl_easy_perform(curl); 

 

    if(code != CURLE_OK)

    {

        cout << "curl_easy_perform failed: "<< code;

    }

    curl_easy_cleanup(curl);

    return buffer;

}

 

void Curlplus::_setCurlopt(CURL *curl,const string& url) const {

    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);

    curl_easy_setopt(curl, CURLOPT_HEADER, 0);

    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

    curl_easy_setopt(curl, CURLOPT_TIMEOUT, _defaulttimeout);

    //curl_easy_setopt(curl, CURLOPT_VERBOSE, true);

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Curlplus::writer);

    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);

 

}

目录
相关文章
|
10月前
|
JSON 监控 API
掌握使用 requests 库发送各种 HTTP 请求和处理 API 响应
本课程全面讲解了使用 Python 的 requests 库进行 API 请求与响应处理,内容涵盖环境搭建、GET 与 POST 请求、参数传递、错误处理、请求头设置及实战项目开发。通过实例教学,学员可掌握基础到高级技巧,并完成天气查询应用等实际项目,适合初学者快速上手网络编程与 API 调用。
960 130
|
11月前
|
JSON API 开发者
淘宝店铺的所有商品API接口,Curl返回数据
淘宝平台未开放获取全店商品的公共API,开发者可通过阿里妈妈的淘宝联盟API获取参与推广的商品。需成为联盟开发者、创建应用,并通过adzone_id关联店铺。使用taobao.tbk.shop.get和taobao.tbk.item.info.get接口,可获取商品列表及详情,但需注意签名生成、调用频率限制及合规要求。未参与推广的商品无法通过该方式获取。
|
11月前
|
域名解析 JSON API
【干货满满】如何处理requests库调用API接口时的异常情况
在调用 API 时,网络波动、服务器错误、参数异常等情况难以避免。本文提供一套系统化的异常处理方案,涵盖 requests 库常见异常类型、处理策略、实战代码与最佳实践,通过分类处理、重试机制与兜底策略,提升接口调用的稳定性与可靠性。
|
自动驾驶 程序员 API
告别重复繁琐!Apipost参数描述库让API开发效率飙升!
在API开发中,重复录入参数占用了42%的时间,不仅效率低下还易出错。Apipost推出的参数描述库解决了这一痛点,通过智能记忆功能实现参数自动填充,如版本号、分页控制、用户信息等常用字段,大幅减少手动输入。支持Key-Value与Raw-Json格式导入,一键提取响应结果至文档,将创建20参数接口文档时间从18分钟缩短至2分钟。相比Postman需手动搜索变量,Apipost的参数复用响应速度仅0.3秒,且支持跨项目共享与实时纠错,真正实现“一次定义,终身受益”。
|
9月前
|
Ubuntu API C++
C++标准库、Windows API及Ubuntu API的综合应用
总之,C++标准库、Windows API和Ubuntu API的综合应用是一项挑战性较大的任务,需要开发者具备跨平台编程的深入知识和丰富经验。通过合理的架构设计和有效的工具选择,可以在不同的操作系统平台上高效地开发和部署应用程序。
329 11
|
监控 API 计算机视觉
CompreFace:Star6.1k,Github上火爆的轻量化且强大的人脸识别库,api,sdk都支持
CompreFace 是一个在 GitHub 上拥有 6.1k Star 的轻量级人脸识别库,支持 API 和 SDK。它由 Exadel 公司开发,基于深度学习技术,提供高效、灵活的人脸识别解决方案。CompreFace 支持多种模型(如 VGG-Face、OpenFace 和 Facenet),具备多硬件支持、丰富的功能服务(如人脸检测、年龄性别识别等)和便捷的部署方式。适用于安防监控、商业领域和医疗美容等多个场景。
2038 4
|
Linux 编译器 vr&ar
Linux的动态库与静态库
静态库在编译时直接嵌入到最终的可执行文件中。
270 0
|
XML 网络协议 API
从cURL到GraphQL:不同API类型概述
本文概述了不同API类型及其应用,帮助开发人员选择合适的工具。cURL是强大的命令行工具,适用于调试和自动化;RESTful API基于HTTP方法,适合Web服务和微服务架构;SOAP用于企业级应用,提供高安全性;GraphQL通过精确查询减少数据传输;WebSocket支持实时通信,适用于低延迟场景。了解这些API的特点和优势,有助于构建高效、可扩展的应用程序。
|
Ubuntu Linux 开发者
Ubuntu20.04搭建嵌入式linux网络加载内核、设备树和根文件系统
使用上述U-Boot命令配置并启动嵌入式设备。如果配置正确,设备将通过TFTP加载内核和设备树,并通过NFS挂载根文件系统。
935 15