카테고리 없음

[curl] cURL - HTTP GET요청 함수 만들어 보기

개발자_이훈규 2014. 1. 16. 23:10

#include <iostream>
#include <string>
#include <curl/curl.h>

using namespace std;

size_t write_html(void *ptr, size_t size, size_t count, void *stream) {

    ( (string*)stream )->append( (char*)ptr, 0, size*count);
    return size * count;
}

bool http_get(char *url, string &html) {
    CURL *curl;
    CURLcode res;
    curl = curl_easy_init();

    if( curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        res = curl_easy_perform(curl);

        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_html);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &html);
        curl_easy_cleanup(curl);

        return true;
    } else {
        return false;
    }

}

int main(int argc, char* argv[] )
{

    string html;
    if(http_get("http://www.google.com", html))
        cout << html << endl;

    return 0;

}



compile하는 방법