{"id":4629,"date":"2022-01-31T15:35:22","date_gmt":"2022-01-31T06:35:22","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4629"},"modified":"2022-01-31T16:35:34","modified_gmt":"2022-01-31T07:35:34","slug":"libcurl-%ec%82%ac%ec%9a%a9%eb%b2%95","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4629","title":{"rendered":"libcurl \uc0ac\uc6a9\ubc95"},"content":{"rendered":"<h1>libcurl \uc0ac\uc6a9\ubc95<\/h1>\n<p>\ucf5c\ubc31 \ubc29\uc2dd\uc73c\ub85c \uc791\ub3d9\ud558\ub294 \ub77c\uc774\ube0c\ub7ec\ub9ac\uc774\ub2e4.<\/p>\n<p>\ucf5c\ubc31 \ud568\uc218\uc5d0\uc11c \ub9ac\ud134 \ub370\uc774\ud0c0\ub97c \uba54\ubaa8\ub9ac \ub610\ub294 \ud30c\uc77c\ub85c \uc800\uc7a5\ud560 \uc218 \uc788\ub2e4.<\/p>\n<p>\ucf5c\ubc31 \ud568\uc218\ub97c \uc9c0\uc815\ud558\uc9c0 \uc54a\uc73c\uba74 stdout \uc73c\ub85c \ub9ac\ud134 \ub370\uc774\ud0c0\uac00 \ucd9c\ub825\ub41c\ub2e4.<\/p>\n<h2>install<\/h2>\n<pre><code class=\"language-bash\">sudo apt-get install libcurl4-openssl-dev<\/code><\/pre>\n<h2>\uc0ac\uc6a9\ubc95<\/h2>\n<h3>METHOD : GET<\/h3>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;curl\/curl.h&gt;\n\nint main() {\n\n    char *url_post = &quot;https:\/\/google.com\/&quot;;\n    char *url_get = &quot;https:\/\/google.com\/&quot;;\n\n    CURL *curl;\n    CURLcode res;\n    curl = curl_easy_init();\n\n    struct curl_slist *list = NULL;\n\n    \/\/ =========================================\n    \/\/ METHOD : GET\n    if(curl) {\n\n        \/\/ URL\n        curl_easy_setopt(curl, CURLOPT_URL, url_get);\n\n        \/\/ METHOD\n        \/\/ DEFAULT : GET\n\n        \/\/ EXECUTE\n        res = curl_easy_perform(curl);\n\n        curl_easy_cleanup(curl);\n\n        \/\/ callback \ud568\uc218\uac00 \uc5c6\uc73c\uba74 stdout \uc73c\ub85c \ucd9c\ub825\ub41c\ub2e4.\n    }\n}<\/code><\/pre>\n<h3>METHOD : POST<\/h3>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n#include &lt;curl\/curl.h&gt;\n\nint main() {\n\n    char *url_post = &quot;https:\/\/google.com\/&quot;;\n    char *url_get = &quot;https:\/\/google.com\/&quot;;\n\n    CURL *curl;\n    CURLcode res;\n    curl = curl_easy_init();\n\n    struct curl_slist *list = NULL;\n\n    curl = curl_easy_init();\n\n    \/\/ =========================================\n    \/\/ METHOD : POST\n    if(curl) {\n\n        list = NULL;\n\n        \/\/ URL\n        curl_easy_setopt(curl, CURLOPT_URL, url_post);\n\n        \/\/ METHOD\n        curl_easy_setopt(curl, CURLOPT_POST, 1L);\n\n        \/\/ HEADERS\n        list = curl_slist_append(list, &quot;Content-Type: application\/json&quot;);\n        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);\n\n        \/\/ SSL\n        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);\n        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1L);\n\n        \/\/ DATA\n        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, &quot;data&quot;);\n\n        \/\/ EXECUTE\n        res = curl_easy_perform(curl);\n\n        curl_easy_cleanup(curl);\n\n        \/\/ callback \ud568\uc218\uac00 \uc5c6\uc73c\uba74 stdout \uc73c\ub85c \ucd9c\ub825\ub41c\ub2e4.\n    }\n}<\/code><\/pre>\n<h3>METHOD : POST &amp; CALLBACK<\/h3>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n#include &lt;curl\/curl.h&gt;\n\nstruct MemoryStruct {\n    char *memory;\n    size_t size;\n};\n\nsize_t write_to_memory_callback(void *buffer, size_t size, size_t nmemb, void *userp) {\n\n    size_t realsize = size * nmemb;\n    struct MemoryStruct *mem = (struct MemoryStruct *) userp;\n    char *ptr = (char *) realloc(mem-&gt;memory, mem-&gt;size + realsize + 1);\n\n    if(!ptr) {\n        printf(&quot;not enough memory (realloc returned NULL)\\n&quot;);\n        return 0;\n    }\n\n    mem-&gt;memory = ptr;\n    memcpy(&amp;(mem-&gt;memory[mem-&gt;size]), buffer, realsize);\n    mem-&gt;size += realsize;\n    mem-&gt;memory[mem-&gt;size] = 0;\n\n    return realsize;\n}\n\nint main() {\n\n    char *url_post = &quot;https:\/\/google.com\/&quot;;\n    char *url_get = &quot;https:\/\/google.com\/&quot;;\n\n    CURL *curl;\n    CURLcode res;\n    curl = curl_easy_init();\n\n    struct curl_slist *list = NULL;\n\n    \/\/ =========================================\n    \/\/ METHOD : POST &amp; CALLBACK\n    if(curl) {\n\n        list = NULL;\n\n        struct MemoryStruct chunk;\n        chunk.memory = (char *) malloc(1);\n        chunk.size = 0;\n\n        \/\/ URL\n        curl_easy_setopt(curl, CURLOPT_URL, url_post);\n\n        \/\/ METHOD\n        curl_easy_setopt(curl, CURLOPT_POST, 1L);\n\n        \/\/ HEADERS\n        list = curl_slist_append(list, &quot;Content-Type: application\/json&quot;);\n        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);\n\n        \/\/ SSL\n        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);\n        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1L);\n\n        \/\/ DATA\n        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, &quot;data&quot;);\n\n        \/\/ CALLBACK\n        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_memory_callback);\n        curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &amp;chunk);\n\n        \/\/ EXECUTE\n        res = curl_easy_perform(curl);\n\n        curl_easy_cleanup(curl);\n\n        if (chunk.size &gt; 0) {\n            printf(&quot;111111111 : %s&quot;, chunk.memory);\n        }\n        free(chunk.memory);\n    }\n}<\/code><\/pre>\n<h2>\uc2e4\ud589\ud558\uae30<\/h2>\n<pre><code class=\"language-bash\">gcc test2.c -lcurl\n.\/a.out<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>libcurl \uc0ac\uc6a9\ubc95 \ucf5c\ubc31 \ubc29\uc2dd\uc73c\ub85c \uc791\ub3d9\ud558\ub294 \ub77c\uc774\ube0c\ub7ec\ub9ac\uc774\ub2e4. \ucf5c\ubc31 \ud568\uc218\uc5d0\uc11c \ub9ac\ud134 \ub370\uc774\ud0c0\ub97c \uba54\ubaa8\ub9ac \ub610\ub294 \ud30c\uc77c\ub85c \uc800\uc7a5\ud560 \uc218 \uc788\ub2e4. \ucf5c\ubc31 \ud568\uc218\ub97c \uc9c0\uc815\ud558\uc9c0 \uc54a\uc73c\uba74 stdout \uc73c\ub85c \ub9ac\ud134 \ub370\uc774\ud0c0\uac00 \ucd9c\ub825\ub41c\ub2e4. install sudo apt-get install libcurl4-openssl-dev \uc0ac\uc6a9\ubc95 METHOD : GET #include &lt;stdio.h&gt; #include &lt;curl\/curl.h&gt; int main() { char *url_post = &quot;https:\/\/google.com\/&quot;; char *url_get = &quot;https:\/\/google.com\/&quot;; CURL *curl; CURLcode res; curl =\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4629\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-4629","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4629"}],"version-history":[{"count":8,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4629\/revisions"}],"predecessor-version":[{"id":4637,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4629\/revisions\/4637"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}