mysql json curl_如何用CURL并解释JSON

CURL *curl;

CURLcode res;

struct curl_slist *headers=NULL; // init to NULL is important

headers = curl_slist_append(headers, "Accept: application/json");

curl = curl_easy_init();

if(curl) {

curl_easy_setopt(curl, CURLOPT_URL, "http://web.com/api/json/123");//cant get json file

curl_easy_setopt(curl, CURLOPT_URL, "http://web.com/pages/123.html");//this returns entire webpage

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

curl_easy_setopt(curl, CURLOPT_RETURNTRANSFER, true);

res = curl_easy_perform(curl);

if(CURLE_OK == res) {

char *ct;

// ask for the content-type

res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);

if((CURLE_OK == res) && ct)

printf("We received Content-Type: %s\n", ct);

}

}

// always cleanup

curl_easy_cleanup(curl);

//参考答案1

std::string ServerContent::DownloadJSO

N(std::string URL)

{

CURL *curl;

CURLcode res;

struct curl_slist *headers=NULL; // init to NULL is important

std::ostringstream oss;

curl_slist_append(headers, "Accept: application/json");

curl_slist_append( headers, "Content-Type: application/json");

curl_slist_append( headers, "charsets: utf-8");

curl = curl_easy_init();

if(curl) {

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

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

curl_easy_setopt(curl, CURLOPT_HTTPGET,1);

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,writer);

res = curl_easy_perform(curl);

if(CURLE_OK == res) {

char *ct;

res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);

if((CURLE_OK == res) && ct)

return *DownloadedResponse;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值