嵌入式 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);

 

}

目录
相关文章
|
26天前
|
JSON API 开发者
淘宝店铺的所有商品API接口,Curl返回数据
淘宝平台未开放获取全店商品的公共API,开发者可通过阿里妈妈的淘宝联盟API获取参与推广的商品。需成为联盟开发者、创建应用,并通过adzone_id关联店铺。使用taobao.tbk.shop.get和taobao.tbk.item.info.get接口,可获取商品列表及详情,但需注意签名生成、调用频率限制及合规要求。未参与推广的商品无法通过该方式获取。
|
5月前
|
自动驾驶 程序员 API
告别重复繁琐!Apipost参数描述库让API开发效率飙升!
在API开发中,重复录入参数占用了42%的时间,不仅效率低下还易出错。Apipost推出的参数描述库解决了这一痛点,通过智能记忆功能实现参数自动填充,如版本号、分页控制、用户信息等常用字段,大幅减少手动输入。支持Key-Value与Raw-Json格式导入,一键提取响应结果至文档,将创建20参数接口文档时间从18分钟缩短至2分钟。相比Postman需手动搜索变量,Apipost的参数复用响应速度仅0.3秒,且支持跨项目共享与实时纠错,真正实现“一次定义,终身受益”。
|
7月前
|
监控 API 计算机视觉
CompreFace:Star6.1k,Github上火爆的轻量化且强大的人脸识别库,api,sdk都支持
CompreFace 是一个在 GitHub 上拥有 6.1k Star 的轻量级人脸识别库,支持 API 和 SDK。它由 Exadel 公司开发,基于深度学习技术,提供高效、灵活的人脸识别解决方案。CompreFace 支持多种模型(如 VGG-Face、OpenFace 和 Facenet),具备多硬件支持、丰富的功能服务(如人脸检测、年龄性别识别等)和便捷的部署方式。适用于安防监控、商业领域和医疗美容等多个场景。
663 4
|
3月前
|
Linux 编译器 vr&ar
Linux的动态库与静态库
静态库在编译时直接嵌入到最终的可执行文件中。
80 0
|
9月前
|
存储 编译器 Linux
动态链接的魔法:Linux下动态链接库机制探讨
本文将深入探讨Linux系统中的动态链接库机制,这其中包括但不限于全局符号介入、延迟绑定以及地址无关代码等内容。
1832 141
|
6月前
|
XML 网络协议 API
从cURL到GraphQL:不同API类型概述
本文概述了不同API类型及其应用,帮助开发人员选择合适的工具。cURL是强大的命令行工具,适用于调试和自动化;RESTful API基于HTTP方法,适合Web服务和微服务架构;SOAP用于企业级应用,提供高安全性;GraphQL通过精确查询减少数据传输;WebSocket支持实时通信,适用于低延迟场景。了解这些API的特点和优势,有助于构建高效、可扩展的应用程序。
|
10月前
|
机器人 API
随机昵称网名[百万昵称库]免费API接口教程
该API接口用于随机生成网名,适用于机器人昵称、虚拟用户名等场景。支持POST和GET请求,需提供用户ID和KEY。返回状态码及信息提示,示例如下:{&quot;code&quot;:200,&quot;msg&quot;:&quot;豌豆公主&quot;}。详情见官方文档:https://www.apihz.cn/api/zicisjwm.html
|
10月前
|
JSON API 数据格式
随机头像图片[API盒子官方资源库]免费API接口教程
API盒子提供的头像资源接口,包含大量网络公开收集的头像,适合非商业用途。支持POST/GET请求,需提供用户ID、KEY及返回格式类型。返回数据包括状态码和消息内容,支持JSON/TXT格式。更多详情见API盒子官网。
|
10月前
|
JSON API 数据格式
随机壁纸图片[API盒子官方资源库]免费API接口教程
API盒子提供的图片资源接口,含数十万张网络公开图片(非商用)。通过POST或GET请求,需提交用户ID、KEY、返回格式及图片类型等参数。返回数据包括状态码和图片地址或错误信息。 示例ID与KEY共享调用限制,建议使用个人ID与KEY。详情见API文档。