示例#1
0
char * json_fetch(char *url)
{
    CURL *curl = curl_easy_init();
    log_null(curl);

    curl_easy_setopt(curl, CURLOPT_URL, url);

    buf_t *buf = buf_size(NULL, BUFFER_SIZE);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fetch_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, buf);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "jsmn-example (https://github.com/alisdair/jsmn-example, [email protected])");

    struct curl_slist *hs = curl_slist_append(NULL, "Accept: application/json");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hs);

    CURLcode res = curl_easy_perform(curl);
    if (res != CURLE_OK)
        log_die("curl_easy_perform failed: %s", curl_easy_strerror(res));

    curl_easy_cleanup(curl);
    curl_slist_free_all(hs);

    char *js = buf_tostr(buf);
    free(buf->data);
    free(buf);

    return js;
}
示例#2
0
文件: main.c 项目: pashashocky/vkp
char * jsonFetch(char *url)
{
    CURL *curl = curl_easy_init();
		buf_t *buf = buf_size(NULL, BUFFER_SIZE);
    
    curl_easy_setopt(curl, CURLOPT_URL, url);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, jsonFetchData);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, buf);
		curl_easy_setopt(curl, CURLOPT_USERAGENT, "vkp (https://github.com/pashashocky), pashashocky");
		
		struct curl_slist *hs = curl_slist_append(NULL, "Accept: application/json");
		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hs);

    CURLcode res = curl_easy_perform(curl);
		if (res != CURLE_OK)
			log_err("curl_easy_perform failed: %s", curl_easy_strerror(res));


    curl_easy_cleanup(curl);
    curl_slist_free_all(hs);

		char *js = buf_tostr(buf);
		free(buf->data);
		free(buf);

		/* printf("%s\n", js); */

    return js;
    
}