频道栏目
首页 > 程序开发 > 软件开发 > C语言 > 正文
C语言使用libcurl与json-c方法(代码实例)
2017-12-04 10:54:22      个评论    来源:_风  
收藏   我要投稿

C语言使用libcurl与json-c方法(代码实例)

#include 
#include 
#include 
#include 
#include 
#include 

struct curl_return_string {
    char *str;
    size_t len;
    size_t size;
}; // 用于存curl返回的结果

size_t _recive_data_from_http_api(void *buffer, size_t size, size_t nmemb, void *user_p)
{
*/
    struct curl_return_string *result_t = (struct curl_return_string *)user_p;

    if (result_t->size < ((size * nmemb) + 1)) {
        result_t->str = (char *)realloc(result_t->str, (size * nmemb) + 1);
        if (result_t->str == NULL) {
            return 0;
        }
        result_t->size = (size * nmemb) + 1;
    }

    result_t->len = size * nmemb;
    memcpy(result_t->str, buffer, result_t->len);
    result_t->str[result_t->len] = '\0';

    return result_t->len;
}

int get_mail_signature_status(char *url, char *subject, char *body)
{
    int ret_mail = 0;
    // 声明保存返回 http 的结果
    struct curl_return_string curl_result_t;

    curl_result_t.len = 0;
    curl_result_t.str = (char *)calloc(1, 1024);
    if (curl_result_t.str == NULL) {
        log_error("calloc fail for curl_result_t.str");
        return 0;
    }
    curl_result_t.size = 1024;

    curl_global_init(CURL_GLOBAL_ALL);

    CURL *curl;
    CURLcode ret;

    // init curl
    curl = curl_easy_init();
    if (!curl) {
        log_error("couldn't init curl");
        return 0;
    }

    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_POST, 1);    // use post 

    // urlencode post data
    char *subject_encode = curl_easy_escape(curl, subject, strlen(subject));
    if (!subject_encode) {
        log_error("urlencode subject fail, so use source data");
        subject_encode = subject;
    }

    char *body_encode = curl_easy_escape(curl, body, strlen(body));
    if (!body_encode) {
        log_error("urlencode body fail, so use source data");
        body_encode = body;
    }

    int request_data_len = 18 + strlen(subject_encode) + strlen(body_encode) + 2;
    char *request_data = (char *)calloc(1, request_data_len) ;
    if (request_data == NULL) {
        log_error("calloc fail for request_data");
        curl_easy_cleanup(curl);
        curl_global_cleanup();

        return 0;
    }

    snprintf(request_data, request_data_len, "subject=%s&content=%s", subject_encode, body_encode);
    log_debug("request data:%s\n", request_data);

    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request_data);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _recive_data_from_http_api);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &curl_result_t);  // 传这个参数过去

    ret = curl_easy_perform(curl);
    if(ret != CURLE_OK) {
        log_error("curl_easy_perform() failed:%s, url:%s", curl_easy_strerror(ret), url);
    } else {
        if (curl_result_t.str) {
            log_info("request url:%s response:%s", url, curl_result_t.str);

            // parse result with json
            // {"err_code":200,"err_msg": "this is no error", "sign":11281921309418229, "status": 1}'
            struct array_list *tmp_array;
            struct json_object *tmp_obj;
            struct json_object *result_json;

            result_json = json_tokener_parse(curl_result_t.str);

            tmp_obj = json_object_object_get(result_json, "err_code");
            int err_code = json_object_get_int(tmp_obj);
            if (err_code == 0) {
                tmp_obj = json_object_object_get(result_json, "sign");
                const char *key = json_object_get_string(tmp_obj);

                tmp_obj = json_object_object_get(result_json, "status");
                int status = json_object_get_int(tmp_obj);

                log_info("key[%s] status[%d]\n", key, status);

                // status: 0:非垃圾;1:垃圾邮件; 2: 疑似垃圾
                ret_mail = status;
            } else {
                log_info("call signature api fail:%s", curl_result_t.str);

                ret_mail = 0;
            }
        }
    }

    if (subject_encode != NULL) {
        free(subject_encode);
        subject_encode = NULL;
    }
    if (body_encode != NULL) {
        free(body_encode);
        body_encode = NULL;
    }
    if (request_data != NULL) {
        free(request_data);
        request_data = NULL;
    }
    if (curl_result_t.str != NULL) {
        free(curl_result_t.str);
        curl_result_t.str = NULL;
        curl_result_t.len = 0;
        curl_result_t.size = 0;
    }

    curl_easy_cleanup(curl);
    curl_global_cleanup();

    return ret_mail;
}

int main(int argc, char **argv)
{
    char signature_url[] = "https://10.29.11.110/monitor/curl_test.php";
    
    int ret = get_mail_signature_status(signature_url, argv[1], argv[2]);
    switch (ret) {
        case 0:
            printf("is good mail\n");
            break;
        case 1:
            printf("is bad mail\n");
            break;
        case 2:
            printf("is unknown mail\n");
            break;
        default:
            printf("has error\n");
            break;
    };

    return 0;
}
点击复制链接 与好友分享!回本站首页
上一篇:使用C语言进行内存管理模拟实验
下一篇:最后一页
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站