libcurl是一个跨平台的网络协议库,支持http,https,ftp,gopher,telnet,dict,file和ldap协议.libcurl同样支持HTTPS证书授权,HTTP POST,HTTP PUT,FTP上传,HTTP基本表单上传,代理和用户认证。
本文主要记录的是使用libcurl编写嵌入式平台调用开发平台提供restful API所需的posts get这两个接口。
具体代码可直接看第三节,详细的libcurl知识可参考第四节参考链接
1 安装
1.1 环境
libcurl版本:7.58.0
openssl版本:1.0.2g
安装环境:Linux x64
libcurl和openssl版本很重要,有一个匹配关系。
1.2 安装
./configure --prefix=/home/jw.li/work/face/libcurl/curl --with-ssl=/home/jw.li/code/save/ssl
Make
Make intall
最后在安装目录下的到lib 和include
2 使用
2.1 一些基础的函数
curl_global_init() //初始化libcurl
curl = curl_easy_init(); //得到curl的指针
curl_easy_setopt() //设置http参数 重要
curl_easy_perform(curl); //执行http请求
curl_easy_cleanup(curl); //回收curl指针
2.2 curl_easy_setopt()常用的参数
CURLOPT_URL http访问的url
CURLOPT_WRITEFUNCTION 回调函数原型为:size_t function( void *ptr, size_t size, size_t nmemb, void *stream)
CURLOPT_WRITEDATA 用于表明CURLOPT_WRITEFUNCTION函数中的stream指针的来源
CURLOPT_COOKIE 字符串类型,设置http头中的cookie信息。
CURLOPT_POSTFIELDS 字符串类型,提交http的post操作字符串数据。
CURLOPT_HTTPHEADER 选项自定义请求头信息
CURLOPT_POSTFIELDSIZE 指定post长度
2.3 编程模型
curl = curl_easy_init(); 初始化
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str()); 设置相应http参数
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)response); 设置响应内容写入buffer
ret = curl_easy_perform(curl); //执行请求 得到结果
3 代码
#include <iostream>
#include <curl/curl.h>
#include <string>
#include <map>
using namespace std;
//回调函数 得到响应内容
int write_data(void* buffer, int size, int nmemb, void* userp){
std::string * str = dynamic_cast<std::string *>((std::string *)userp);
str->append((char *)buffer, size * nmemb);
return nmemb;
}
int posts(string url, string &body, string* response);
int get(string url, string* response);
int main(int argc, char** argv){
std::map<std::string, std::string> data;
std::string body;
std::string response;
int status_code = get("http://www.baidu.com", &response);
if (status_code != CURLcode::CURLE_OK) {
return -1;
}
cout << response << endl;
cout << "*********************************" << endl;
status_code = posts("https://www.zhihu.com", body, &response);
if (status_code != CURLcode::CURLE_OK) {
return -1;
}
cout << response << endl;
return 0;
}
int get(string url, string* response)
{
CURL *curl;
CURLcode ret;
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: Agent-007");
curl = curl_easy_init(); // 初始化
if (curl)
{
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); //改协议头
curl_easy_setopt(curl, CURLOPT_URL, (char *)url.c_str()); // 指定url
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)response);
int status_code = curl_easy_perform(curl); // 执行
ret = curl_easy_perform(curl); //执行请求
if(ret == 0){
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
return status_code;
}
else{
return ret;
}
}
else{
return -1;
}
}
int posts(string url, string &body, string* response)
{
CURL *curl;
CURLcode ret;
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str()); //指定post内容
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, body.size()); //指定post长度
curl_easy_setopt(curl, CURLOPT_HEADER, 0); //设置协议头
// curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_URL, (char *)url.c_str()); //指定url
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); //绑定相应
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)response); //绑定响应内容的地址
ret = curl_easy_perform(curl); //执行请求
if(ret == 0){
curl_easy_cleanup(curl);
return 0;
}
else{
return ret;
}
}
else{
return -1;
}
}
编译:
g++ -I../libcurl/include -std=c++11 ./test_main.cpp -L../libcurl/lib -lcurl -lssl -lcrypto -O2 -ldl -o sample_X86_64
4 参考链接
https://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html
https://www.cnblogs.com/lidabo/p/4583061.html