반응형


+LIBCURL 라이브러리로 POST data 전송하기 또는 curl 커맨드로 전송하기

https도 가능함.


#include <curl/curl.h>

#include <syslog.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>


#define CURLCMD "curl"


struct PostResult {

size_t size;

char* ptr;

};



void init_string(struct PostResult *s)

{

s->size = 0;

s->ptr = malloc(s->size + 1);

if(s->ptr == NULL) {

fprintf(stderr," malloc() fail\n");

exit(EXIT_FAILURE);

}

s->ptr[0] = 0;

}


#ifdef _CURL_LIB_

static size_t

WriteCallback(void* ptr, size_t size, size_t nmemb, struct PostResult *s)

{

size_t new_len = s->size + size * nmemb;

s->ptr = realloc(s->ptr, new_len + 1);

if(s->ptr == NULL) {

fprintf(stderr, "ralloc fail\n");

exit(EXIT_FAILURE);

}

memcpy(s->ptr + s->size, ptr, size*nmemb);

s->ptr[new_len] = 0;

s->size = new_len;

return size*nmemb;

}

#endif


int Post_Send(char* url, char* jsonMsg)

{

#ifdef _CURL_LIB_

int i=0;

CURL *curl=NULL;

CURLcode res;

#endif

char inmsg[1024]={0,};

struct PostResult repost;

int maxsize=0 ;


// cmdline method

#ifdef _CURL_CMD_

char inmsg2[1024]={0,};

char cmdline[4096]={0,};

char result[1024]={0,};

#endif


if(!url || !jsonMsg) {

return -1;

}


init_string(&repost);


memset(inmsg, 0, sizeof(inmsg));

maxsize = strlen(jsonMsg)>sizeof(inmsg) ? sizeof(inmsg) : strlen(jsonMsg) ;

memcpy(inmsg, jsonMsg, maxsize) ;


// CURL 라이브러리 방식

#ifdef _CURL_LIB_

curl = curl_easy_init();

if(curl)

{

const int timeout = 30000;

i = strlen(inmsg);

curl_easy_setopt(curl, CURLOPT_URL, url);

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, inmsg);

curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(inmsg));

curl_easy_setopt(curl, CURLOPT_POST, 1L);

curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout/1000);

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);

curl_easy_setopt(curl, CURLOPT_WRITEDATA, &repost);


res = curl_easy_perform(curl);

if(CURLE_OK != res)

{

// server error

return 1;

}


if(repost.size > 1){

// 수신 데이터 주소 : repost.ptr

curl_easy_cleanup(curl);

return -1;

}else{

printf("error: %s\n", strerror(res));

curl_easy_cleanup(curl);

// server error

            return 1;

}

curl_easy_cleanup(curl);

}

#endif


// CURL 커맨드 방식

#ifdef _CURL_CMD_

{

int contimeout=10 ;  // secs

FILE *fp=NULL ;

int index=0 ;

int rbytes=0 ;


memset(inmsg2, 0, sizeof(inmsg2)) ;

q2dq(inmsg, inmsg2, sizeof(inmsg2)) ;    // " -> \"로 변경하는 함수

snprintf(cmdline, sizeof(cmdline)-1, "%s --connect-timeout %d -s -d \"%s\" -k \"%s\"",

  CURLCMD, contimeout, inmsg2, url) ;


fp = popen(cmdline, "r") ;

if ( fp==NULL ) {

// server error

}

else {

do {

rbytes=fread(result+index, 1, sizeof(result)-index-1, fp) ;

#ifdef _DEBUG

printf("rbytees=%d\n", rbytes) ;

#endif

if ( rbytes<=0 )

break; 

index+=rbytes ;

} while(rbytes!=0 );

pclose(fp) ;


#ifdef _DEBUG

printf("rcved=%s\n", result) ;

#endif

if( strlen(result)>1 ){

// 수신 데이터 주소: result

} else {

strcpy(tqResult->result, "100") ; // server error

}

#ifdef _DEBUG

printf("success post request\n");

#endif

}

}

#endif


return 0;

}



일부 함수는 없지만 적절히 수정하면 사용할 수 있다.


예를 들어 보내는 데이터를 "submit=1" 이렇게 하면 POST 수신쪽에서는 php의 경우 $_POST["submit"] 으로 받을 수 있다. 

form data가 아닌 raw data를 그대로 받으려면 

$post_raw_data = file_get_contents("php://input") ;

이렇게 한다.


다른 예제)

https://curl.haxx.se/libcurl/c/post-callback.html




+ Recent posts