Calibration_Stand/backend/Libs/CURL/Src/curl2.c

80 lines
2.2 KiB
C
Raw Permalink Normal View History

2024-11-26 10:17:15 +07:00
#include "../Inc/curl2.h"
size_t write_callback_size(void *ptr, size_t size, size_t nmemb, char *data) {
return size * nmemb;
}
size_t write_callback(void *ptr, size_t size, size_t nmemb, char *data) {
size_t total_size = size * nmemb;
strncat(data, ptr, total_size);
return total_size;
}
void CURL_GET_Request(uint8_t *URL, uint8_t *result){
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, URL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, result);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
}
void CURL_POST_Request(uint8_t *URL, uint8_t *post_data){
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, URL);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
} else {
printf("POST-запрос был успешно отправлен.\n");
}
curl_easy_cleanup(curl);
}
}
uint8_t CURL_Check_connection(uint8_t *URL){
CURL *curl;
CURLcode res;
uint8_t returnCode = 0;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, URL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback_size);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "Connection failed: %s\n", curl_easy_strerror(res));
returnCode = 1;
}
curl_easy_cleanup(curl);
}
else{
fprintf(stderr, "CURL INIT failed.\n");
returnCode = 1;
}
return returnCode;
}