Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Created April 10, 2026 05:24
Show Gist options
  • Select an option

  • Save sunmeat/14faf8f6aec987afa4e360fb233d45d0 to your computer and use it in GitHub Desktop.

Select an option

Save sunmeat/14faf8f6aec987afa4e360fb233d45d0 to your computer and use it in GitHub Desktop.
cpp console app and php integration (http example) mac os version
# треба встановити curl (якщо ще немає)
brew install curl
# якщо немає Homebrew - спочатку треба встановити його:
# /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
======================================================================================================================
#include <iostream>
#include <string>
#include <curl/curl.h>
using namespace std;
// ==================== CurlHandler ====================
class CurlHandler {
public:
CurlHandler() {
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
}
~CurlHandler() {
if (curl) curl_easy_cleanup(curl);
curl_global_cleanup();
}
bool performPostRequest(const string& url, const string& postData, string& response) {
response.clear();
if (!curl) return false;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
// заголовок Content-Type
struct curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
CURLcode res = curl_easy_perform(curl);
curl_slist_free_all(headers); // звільняємо заголовки
return (res == CURLE_OK);
}
private:
CURL* curl = nullptr;
static size_t WriteCallback(void* contents, size_t size, size_t nmemb, string* output) {
size_t total_size = size * nmemb;
output->append(static_cast<char*>(contents), total_size);
return total_size;
}
};
// ==================== ConsoleIO ====================
class ConsoleIO {
public:
static int getInputInt(const string& prompt) {
int value;
cout << prompt;
cin >> value;
cin.ignore(); // очищаємо залишок рядка
return value;
}
static void displayMessage(const string& message) {
cout << message << "\n";
}
};
// ==================== PHPRequestDemo ====================
class PHPRequestDemo {
public:
static void SumTwoNumbers() {
CurlHandler handler;
string url = "http://sunmeat.atwebpages.com/cpp/script.php";
int first = ConsoleIO::getInputInt("Введіть перше значення: ");
int second = ConsoleIO::getInputInt("Введіть друге значення: ");
string postData = "first=" + to_string(first) + "&second=" + to_string(second);
string response;
if (handler.performPostRequest(url, postData, response)) {
ConsoleIO::displayMessage("Відповідь сервера: " + response);
} else {
ConsoleIO::displayMessage("Запит завершився невдачею.");
}
}
static void InsertSQLRow(const string& name) {
CurlHandler handler;
string url = "http://sunmeat.atwebpages.com/cpp/upload.php";
string postData = "name=" + name;
string response;
if (handler.performPostRequest(url, postData, response)) {
ConsoleIO::displayMessage("Рядок успішно додано. Відповідь: " + response);
} else {
ConsoleIO::displayMessage("Не вдалося виконати запит.");
}
}
};
int main() {
setlocale(LC_ALL, "");
PHPRequestDemo::SumTwoNumbers();
PHPRequestDemo::InsertSQLRow("Odesa-KND-241");
cout << "\nНатисніть Enter для виходу...";
cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment